source: trunk/libIGCM/libIGCM_debug/libIGCM_debug.ksh @ 1254

Last change on this file since 1254 was 1254, checked in by sdipsl, 9 years ago
  • Property licence set to
    The following licence information concerns ONLY the libIGCM tools
    ==================================================================

    Copyright © Centre National de la Recherche Scientifique CNRS
    Commissariat à l'Énergie Atomique CEA

    libIGCM : Library for Portable Models Computation of IGCM Group.

    IGCM Group is the french IPSL Global Climate Model Group.

    This library is a set of shell scripts and functions whose purpose is
    the management of the initialization, the launch, the transfer of
    output files, the post-processing and the monitoring of datas produce
    by any numerical program on any plateforme.

    This software is governed by the CeCILL license under French law and
    abiding by the rules of distribution of free software. You can use,
    modify and/ or redistribute the software under the terms of the CeCILL
    license as circulated by CEA, CNRS and INRIA at the following URL
    "http://www.cecill.info".

    As a counterpart to the access to the source code and rights to copy,
    modify and redistribute granted by the license, users are provided only
    with a limited warranty and the software's author, the holder of the
    economic rights, and the successive licensors have only limited
    liability.

    In this respect, the user's attention is drawn to the risks associated
    with loading, using, modifying and/or developing or reproducing the
    software by the user in light of its specific status of free software,
    that may mean that it is complicated to manipulate, and that also
    therefore means that it is reserved for developers and experienced
    professionals having in-depth computer knowledge. Users are therefore
    encouraged to load and test the software's suitability as regards their
    requirements in conditions enabling the security of their systems and/or
    data to be ensured and, more generally, to use and operate it in the
    same conditions as regards security.

    The fact that you are presently reading this means that you have had
    knowledge of the CeCILL license and that you accept its terms.
  • Property svn:keywords set to Revision Author Date
File size: 35.6 KB
Line 
1#!/bin/ksh
2
3#**************************************************************
4# Author: Patrick Brockmann, Martial Mancip
5# Contact: Patrick.Brockmann__at__cea.fr Martial.Mancip__at__ipsl.jussieu.fr
6# $Revision::                                          $ Revision of last commit
7# $Author::                                            $ Author of last commit
8# $Date::                                              $ Date of last commit
9# IPSL (2006)
10#  This software is governed by the CeCILL licence see libIGCM/libIGCM_CeCILL.LIC
11#
12#**************************************************************
13
14#==================================================
15# The documentation of this file can be automatically generated
16# if you use the prefix #D- for comments to be extracted.
17# Extract with command: cat lib* | grep "^#D-" | cut -c "4-"
18#==================================================
19
20#==================================================
21# Add high level verbosity
22typeset -i Verbosity=${Verbosity:=3}
23
24#==================================================
25# DEBUG_debug
26# Add low level verbosity
27DEBUG_debug=${DEBUG_debug:=false}
28
29#==================================================
30# GENERATE RANDOM ERROR ; only apply if ( ${DEBUG_debug} )
31typeset -r RandomError=false
32
33#==================================================
34# NULL_STR
35# Default null string
36typeset -r NULL_STR="_0_"
37
38#==================================================
39# libIGCM_CurrentTag
40# Current libIGCM tag, check compatibilty with *.card
41typeset -r libIGCMVersion="2.7"
42
43#==================================================
44# Exit Flag (internal debug)
45# When true, end the master loop AFTER SAVES FILES
46ExitFlag=false
47
48#==================================================
49# When we start to run the simulation is not finished
50simulationIsOver=false
51
52#==================================================
53# Declare a stack of functions calls
54unset IGCM_debug_Stack
55unset IGCM_debug_StackArgs
56unset IGCM_debug_StackTiming
57IGCM_debug_Stack[0]=${NULL_STR}
58IGCM_debug_StackArgs[0]=${NULL_STR}
59IGCM_debug_StackTiming[0]=${NULL_STR}
60IGCM_debug_LenStack=0
61
62#D-#==================================================================
63#D-function IGCM_debug_getDate_ms
64#D- * Purpose: Give number of milliseconds since 01-jan-1970
65function IGCM_debug_getDate_ms
66{
67  typeset nanosecs ms
68  # nano secondes since 01-jan-1970
69  nanosecs=$( date +%s%N )
70
71  # truncate the last 6 digits to get milliseconds since 01-jan-1970
72  ms=${nanosecs:0:${#nanosecs}-6}
73
74  echo "$ms"
75}
76
77#D-#==================================================================
78#D-function IGCM_debug_sizeOfTabContent
79#D- * Purpose: Give sumed size of a list of files
80#D- * Usage: IGCM_debug_sizeOfTabContent entityList destination
81#D- *        where entityList is a list of files or directory
82#D- *        where dest is either a directory or a file name
83function IGCM_debug_sizeOfTabContent
84{
85  typeset entityListe destination iEntity sizeKo sumSizeKo sumSizeMo
86
87  eval set +A entityListe \${${1}}
88  destination=${2}
89  sumSizeKo=0
90
91  # Here we will try to compute size (file or directory size) from local path and not from archive.
92  for ((i = 0; i < ${#entityListe[*]}; i += 1)) ; do
93    if [ -f ${entityListe[$i]} ] ; then
94      # One file or a bunch of files has been copied without renaming from a visible filesystem
95      iEntity=${entityListe[$i]}
96    elif [ -f ${entityListe[$i]##/*/} ] ; then
97      # One file or a bunch of files has been copied without renaming from an non visible filesystem
98      # remove path /home/login/../ from entityListe elements
99      iEntity=${entityListe[$i]##/*/}
100    elif [ -f ${destination} ] ; then
101      # a file has been copied and renamed
102      iEntity=${destination}
103    elif [ -f ${destination}/${entityListe[$i]##/*/} ] ; then
104      # a copy in a directory but not in ${PWD}
105      iEntity=${destination}/${entityListe[$i]##/*/}
106    elif [ -d ${entityListe[$i]} ] ; then
107      # a directory has been copied from a non remote place
108      iEntity=${entityListe[$i]}
109    elif [ -d ${destination}/${entityListe[$i]##/*/} ] ; then
110      # a directory has been copied from a remote archive and not renamed
111      iEntity=${destination}/${entityListe[$i]##/*/}
112    elif [ -d ${destination} ] ; then
113      # a directory has been copied from a remote archive and renamed
114      iEntity=${destination}
115    fi
116    sizeKo=$( du --apparent-size -skL ${iEntity} | gawk '{print $1}' )
117    sumSizeKo=$(( $sumSizeKo + $sizeKo ))
118  done
119  sumSizeMo=$( echo "scale=6;${sumSizeKo}/1024" | bc )
120  echo "${sumSizeKo}|${sumSizeMo}"
121}
122
123#D-#==================================================================
124#D-function IGCM_debug_send_AMQP_msg__MAILTUNNEL
125#D- * Purpose: Take over AMQP C client using mail as a message recipient
126#D- * One argument : base64 encoded message
127#D- * Attach encoded config.card when starting the simulation
128
129function IGCM_debug_send_AMQP_msg__MAILTUNNEL {
130
131  typeset b64_encoded_msg mail_recipient
132  typeset buffer send_messages mail_frequency
133  typeset last_mail_date__file
134  typeset secondsBetweenRefAndLastMail secondsSinceLastMail
135
136  b64_encoded_msg=$1
137
138  mail_recipient="superviseur@ipsl.jussieu.fr"
139  send_messages=0
140  mail_frequency=3600 # in seconds
141  # use to keep track when was last mail sent (maybe to be replaced with global variable)
142  last_mail_date__file=${R_BUF}/.stamp.${config_UserChoices_TagName}.${config_UserChoices_JobName}
143  # use to accumulate messages before sending them
144  buffer=${R_BUF}/.buffer.${config_UserChoices_TagName}.${config_UserChoices_JobName}
145
146  # init
147  if [ ! -f "${buffer}" ]; then
148    touch ${buffer}
149  fi
150
151  if [ ! -f "${last_mail_date__file}" ]; then
152    touch ${last_mail_date__file}
153  else
154    # compute last time the file was changed (in seconds)
155    secondsBetweenRefAndLastMail=$(stat -c %Y ${last_mail_date__file})
156    status=$?
157    #
158    if [ ${status} -gt 0 ] ; then
159      IGCM_debug_Exit "IGCM_debug_send_AMQP_msg__MAILTUNNEL"
160    fi
161    secondsSinceLastMail=$(( $(date +%s) - ${secondsBetweenRefAndLastMail} ))
162    # send message when exceeding threshold
163    [ ${secondsSinceLastMail} -gt ${mail_frequency} ] && send_messages=1
164  fi
165
166  # queue messages in the buffer
167  echo ${b64_encoded_msg} >> ${buffer}
168
169  # send mail
170
171  if [ X${initBigBro} = Xtrue ] ; then
172    #echo $(date +"%Y-%m-%dT%H:%M:%S.%N%z") > ${SUBMIT_DIR}/mail.txt
173    mailx -s "[TEMPORARY AMQP CHANNEL]" -a ${SUBMIT_DIR}/config.card.base64 ${mail_recipient} < ${buffer} # send buffer
174    rm -f $buffer ; touch ${buffer}                                    # clear buffer
175    touch ${last_mail_date__file}                                      # memorize last mail date
176    initBigBro=false
177  elif [ ${send_messages} -eq 1 ] ; then
178    #echo $(date +"%Y-%m-%dT%H:%M:%S.%N%z") >> ${SUBMIT_DIR}/mail.txt
179    mailx -s "[TEMPORARY AMQP CHANNEL]" ${mail_recipient}  < ${buffer} # send buffer
180    rm -f ${buffer} ; touch ${buffer}                                  # flush the buffer
181    touch ${last_mail_date__file}                                      # memorize last mail date
182  fi
183
184  if ( ${FlushAMQP} ) ; then
185    mailx -s "[TEMPORARY AMQP CHANNEL]" ${mail_recipient}  < ${buffer} # send buffer
186    rm -f ${buffer}                                                    # cleaning behind us
187    rm -f ${last_mail_date__file}                                      # cleaning behind us
188  fi
189
190  # Allways all good for now.
191  return 0
192}
193
194#D-#==================================================================
195#D-function IGCM_debug_sendAMQP_Metrics
196#D- * Purpose: Take over AMQP C client using mail as a message recipient
197#D- * Two arguments : - Directory where metrics.json files can be found
198#D- *                 - Metrics Group Name. metrics will be added to this group
199#D- * Attach encoded metrics.json files.
200
201function IGCM_debug_sendAMQP_Metrics {
202
203  typeset mail_recipient encodedBody
204  if [ X${ActivateBigBro} = Xtrue ] ; then
205    mail_recipient="superviseur@ipsl.jussieu.fr"
206    # Metrics tag on server side
207    code=7100
208    # Usual AMQP message to route messages on server side
209    encodedBody=$( echo "{${genericSimulationID},\"msgCode\":\"${code}\",\"msgUID\":\"$(uuidgen)\",\"metricsGroupName\":\"${2}\",\"msgTimestamp\":\"$( date +"%Y-%m-%dT%H:%M:%S.%N%z" )\"}" |  base64 -w 0 )
210    # send mail
211    attachmentsOptions=""
212    for metricsFile in $( ls $1/*json ) ; do
213      attachmentsOptions="-a ${metricsFile} ${attachmentsOptions}"
214    done
215    IGCM_debug_Print 2 "IGCM_debug_sendAMQP_Metrics "
216    echo ${encodedBody}|mailx -s "[TEMPORARY AMQP CHANNEL]" ${attachmentsOptions} ${mail_recipient}
217  fi
218
219  # Allways all good for now.
220  return 0
221}
222
223#D-#==================================================================
224#D-function IGCM_debug_SendAMQP
225#D- * Purpose: Send body; encoded body and config.card to rabbitMQ
226function IGCM_debug_sendAMQP {
227
228  typeset decal first additionnalOption encodedBody
229
230  # Encode message Body
231  encodedBody=$( echo "${Body}" | base64 -w 0 )
232
233  # Send config.card ?
234  if [ X${1} = Xactivate ] ; then
235    # Encode config.card
236    cat ${SUBMIT_DIR}/config.card | base64 -w 0 > ${SUBMIT_DIR}/config.card.base64
237    # Prepare additionnal option
238    additionnalOption="-f ${SUBMIT_DIR}/config.card.base64"
239    #
240    initBigBro=true
241  else
242    additionnalOption=
243    #
244    initBigBro=false
245  fi
246
247  # Only cosmetics : stack file
248  if [ X${ActivateStackFilling} = Xtrue ] ; then
249    decal=0
250    while [ ${decal} -lt ${IGCM_debug_LenStack} ]; do
251      printf ' ' >> ${StackFileLocation}/${StackFileName}
252      (( decal = decal + 1 ))
253    done
254    # Log to stack file using human readable format
255    echo "${Body}" >> ${StackFileLocation}/${StackFileName}
256  fi
257
258  # Log separately encoded AMQP message command for reuse in a mock up
259  #echo sendAMQPMsg -h localhost -p 5672 ${additionnalOption} -b ${encodedBody} >> ${RUN_DIR_PATH}/send.AMQP.${config_UserChoices_JobName}.${config_UserChoices_ExperimentName}.${config_UserChoices_SpaceName}.${config_UserChoices_TagName}.${CumulPeriod}.history.txt
260
261  # Send the message
262  if [ X${BigBrotherChannel} = XMAIL ] ; then
263    IGCM_debug_send_AMQP_msg__MAILTUNNEL "${encodedBody}"
264    status=$?
265  else
266    sendAMQPMsg -h localhost -p 5672 ${additionnalOption} -b ${encodedBody}
267    status=$?
268  fi
269
270  if [ ${status} -gt 0 ] ; then
271    IGCM_debug_Print 2 "IGCM_debug_Push/PopStack/ActivateBigBro : command sendAMQPMsg failed error code ${status}"
272    echo sendAMQPMsg -h localhost -p 5672 -b "${Body}"
273    exit 1
274  fi
275}
276
277#D-#==================================================================
278#D-function IGCM_debug_CallStack
279#D-* Purpose: Print the call stack tree from the oldest to the youngest (opposite of the display standard)
280#D-
281function IGCM_debug_CallStack {
282  if ( $DEBUG_debug ) ; then
283    # Cosmetics
284    typeset i decal
285    i=0
286    until [ $i -eq ${IGCM_debug_LenStack} ]; do
287      decal=0
288      until [ $decal -eq ${i} ]; do
289        printf -- ' '
290        (( decal = decal + 1 ))
291      done
292      echo "$i - ${IGCM_debug_Stack[$(( $IGCM_debug_LenStack-$i-1 ))]}" "(${IGCM_debug_StackArgs[$(( $IGCM_debug_LenStack-$i-1 ))]})"
293      ((i = i + 1))
294    done
295  fi
296}
297
298#D-#==================================================================
299#D-function IGCM_debug_PushStack
300#D-* Purpose: Push a function name in the stack
301#D-
302function IGCM_debug_PushStack {
303  if ( $DEBUG_debug ) ; then
304    typeset decal inputs startTime_ms
305
306    # Only cosmetics : stack file
307    if [ X${ActivateStackFilling} = Xtrue ] ; then
308      echo >> ${StackFileLocation}/${StackFileName}
309      decal=0
310      while [ ${decal} -lt ${IGCM_debug_LenStack} ]; do
311        printf ' ' >> ${StackFileLocation}/${StackFileName}
312        (( decal = decal + 1 ))
313      done
314
315      # Fill the stack file
316      echo "> ${IGCM_debug_LenStack} : ${@}" >> ${StackFileLocation}/${StackFileName}
317    fi
318
319    # Save input list in an indexed array
320    INPUTS=( $@ )
321
322    # Get timing information
323    startTime_ms=$( IGCM_debug_getDate_ms )
324
325    # We add function call name on beginning of the stack
326    set +A IGCM_debug_Stack -- ${1} ${IGCM_debug_Stack[*]}
327
328    # Save timing in milliseconds in an indexed array
329    set +A IGCM_debug_StackTiming -- ${startTime_ms} ${IGCM_debug_StackTiming[*]}
330
331    # We include the "null" Args in the beginning of the StackArgs
332    set +A IGCM_debug_StackArgs ${NULL_STR} ${IGCM_debug_StackArgs[*]}
333
334    # Then, we shift StackArgs tabular
335    # Replacing blank separated list by comma separated list of quoted elements (except the first and last element)
336    if [ $# -gt 1 ]; then
337      IGCM_debug_StackArgs[0]=$(echo ${INPUTS[*]:1} | sed -e "s/\ /\",\"/g" )
338    fi
339
340    # Increment LenStack
341    (( IGCM_debug_LenStack = IGCM_debug_LenStack + 1 ))
342
343    #IGCM_debug_CallStack
344  fi
345}
346
347#D-#==================================================================
348#D-function IGCM_debug_PopStack
349#D-* Purpose: Pop a function name in the stack
350#D-
351function IGCM_debug_PopStack {
352  if ( $DEBUG_debug ) ; then
353    typeset i decal command arguments startTime_ms endTime_ms
354    typeset instrumentation dest prefix
355    # they are not typeset because they are send "by adress" to son functions
356    # we unset them to avoid "memory effect"
357    unset fileList source
358
359    # INTRODUCE SIMPLE ERROR GENERATOR TO TEST SUPERVISOR
360    # PROBABILITY ERROR IS 0.0001 PER COMMAND OR FUNCTION CALL
361    # THERE ARE ~500 COMMAND OR FUNCTION CALL PER PERIOD
362    if ( ${RandomError} ) ; then
363      if [ $((RANDOM%10000)) -le 10 ] ; then
364        IGCM_debug_Print 1 "Random error has been triggered"
365        if [ X${ActivateStackFilling} = Xtrue ] ; then
366          echo "RANDOM ERROR" >> ${StackFileLocation}/${StackFileName}
367        fi
368        ExitFlag=true
369      fi
370    fi
371
372    if [ "${IGCM_debug_Stack[0]}" = "${1}" ]; then
373      # Everything is cool
374
375      # Get timing information
376      endTime_ms=$( IGCM_debug_getDate_ms )
377
378      # Save Stack information before poping the stack
379      command=${IGCM_debug_Stack[0]}
380
381      # Go from comma separated list of quoted elements (except the first and the last element)
382      # to unquoted space separated elements in an array
383      set -A arguments -- $( echo ${IGCM_debug_StackArgs[0]} | sed -e "s/\",\"/\ /g" )
384
385      # Save Stack information before poping the stack
386      startTime_ms=${IGCM_debug_StackTiming[0]}
387
388      # Pop the stack
389      (( IGCM_debug_LenStack = IGCM_debug_LenStack - 1 ))
390      set -A IGCM_debug_Stack -- ${IGCM_debug_Stack[*]:1}
391      set -A IGCM_debug_StackArgs -- ${IGCM_debug_StackArgs[*]:1}
392      set -A IGCM_debug_StackTiming -- ${IGCM_debug_StackTiming[*]:1}
393    else
394      echo 'IGCM_debug_Exit : stack is corrupted ! LenStack =' ${IGCM_debug_LenStack}
395      IGCM_debug_Exit $@
396    fi
397
398    # Special actions depending on command to prepare IGCM_debug_PrintInfosActions call
399    # We are interested in:
400    #  0. Which command performs the work
401    #  1. Size of entity we are working with
402    #  2. Where are we reading
403    #  3. Where are we writing
404    #  4. How long it took
405
406    instrumentation=false
407
408    case ${command} in
409    # Classical copy (only files are given to IGCM_sys_Cp as options)
410    IGCM_sys_Cp)
411      instrumentation=true
412      # All but the latest
413      fileList=${arguments[*]:0:${#arguments[*]}-1}
414      # just need the first file to get the directory
415      source=${arguments[0]}
416      # Nothing but the latest
417      dest=${arguments[${#arguments[*]}-1]}
418      # Size of file whose name are stored in a list
419      entitySize=$( IGCM_debug_sizeOfTabContent fileList ${dest} )
420      ;;
421
422    # Copy from archive machine or from buffer
423    IGCM_sys_Get|IGCM_sys_GetBuffer)
424      instrumentation=true
425      if [ ${#arguments[*]} -eq 2 ] ; then
426        source=${arguments[0]}
427        dest=${arguments[1]}
428        # Size of file whose name are stored in a variable
429        entitySize=$( IGCM_debug_sizeOfTabContent source ${dest} )
430      elif ( [ ${#arguments[*]} -eq 3 ] && [ ${arguments[0]} = '/l' ] ) ; then
431        # IGCM_sys_Get /l liste_file[*] /ccc/scratch/cont003/dsm/p86denv/RUN_DIR/985998_14754/
432        # Keep the array name hosting the all list
433        eval set +A fileList \${${arguments[1]}}
434        # just need the first file to get the directory
435        source=${fileList[0]}
436        dest=${arguments[2]}
437        # Size of file whose name are stored in a list
438        entitySize=$( IGCM_debug_sizeOfTabContent fileList[*] ${dest} )
439      elif [ [ ${#arguments[*]} -ge 3 ] ; then
440       # All but the latest
441        fileList=${arguments[*]:0:${#arguments[*]}-1}
442        # just need the first file to get the directory
443        source=${arguments[0]}
444        # Nothing but the latest
445        dest=${arguments[${#arguments[*]}-1]}
446        # Size of file whose name are stored in a list
447        entitySize=$( IGCM_debug_sizeOfTabContent fileList ${dest} )
448      fi
449      ;;
450
451    # Copy from compute node or copy to archive/buffer
452    IGCM_sys_Get_Master|IGCM_sys_Get_Dir|IGCM_sys_Put_Out|IGCM_sys_PutBuffer_Out)
453      instrumentation=true
454      source=${arguments[0]}
455      dest=${arguments[1]}
456      # Size of file whose name are stored in a variable
457      entitySize=$( IGCM_debug_sizeOfTabContent source ${dest} )
458      ;;
459
460    # Rebuild command
461    IGCM_sys_rebuild|IGCM_sys_rebuild_station)
462      instrumentation=true
463      # All but the first
464      fileList=${arguments[*]:1:${#arguments[*]}-1}
465      # just need a file to get the directory
466      source=${arguments[1]}
467      # Nothing but the first
468      dest=${arguments[0]}
469      # Size of file whose name are stored in a list
470      entitySize=$( IGCM_debug_sizeOfTabContent fileList ${dest} )
471      ;;
472
473    # NCO commands
474    IGCM_sys_ncrcat|IGCM_sys_ncecat|IGCM_sys_ncra|IGCM_sys_ncks|IGCM_sys_cdo)
475      # Example of what we want to catch : only filenames in those command lines
476      # IGCM_sys_ncrcat -O -v ${list_var_final_ncrcat} ${OUT_SE[*]} ${RESULT_SE}
477      # IGCM_sys_ncrcat --hst -v ${liste_coord}${var} ${file1} ${liste_file_tmp[*]} ${file_out}
478      # IGCM_sys_ncrcat -p ${dir} ${liste_file_tmp} --output ${output}
479      # IGCM_sys_ncrcat -x -v ${list_var} -p ${dir} ${liste_file_tmp} --output ${output}
480      instrumentation=true
481      keepGoing=true
482      prefix=.
483      i=0
484      while ( ${keepGoing} ) ; do
485        # the last one is not interesting
486        if [ ${i} -eq ${#arguments[*]}-1 ] ; then
487          keepGoing=false
488        # look after "-p" option. Path prefix is the following arguments
489        elif [ ${arguments[${i}]} = "-p" ] ; then
490          ((i = i + 1))
491          prefix=${arguments[${i}]}
492          ((i = i + 1))
493        elif [ ${i} -eq ${#arguments[*]}-1 ] ; then
494          keepGoing=false
495        # looking for files
496        elif [ -f ${prefix}/${arguments[${i}]} ] ; then
497          fileList="${fileList} ${prefix}/${arguments[${i}]}"
498          ((i = i + 1))
499        # other options are not interesting
500        else
501          ((i = i + 1))
502        fi
503      done
504
505      # i value is at least 1
506      # just need one file to get the directory
507      source=$( echo ${fileList} | gawk '{print $1}' )
508      # Nothing but the latest
509      dest=${arguments[${#arguments[*]}-1]}
510      # Size of file whose name are stored in a list
511      entitySize=$( IGCM_debug_sizeOfTabContent fileList ${dest} )
512      ;;
513    esac
514
515    # Print information related to instrumentation
516    ( ${instrumentation} ) && IGCM_debug_PrintInfosActions ${command} ${entitySize} ${startTime_ms} ${endTime_ms} ${dest} ${source}
517
518    # Only cosmetics : stack file
519    if [ X${ActivateStackFilling} = Xtrue ] ; then
520      decal=0
521      while [ ${decal} -lt ${IGCM_debug_LenStack} ]; do
522        printf ' ' >> ${StackFileLocation}/${StackFileName}
523        (( decal = decal + 1 ))
524      done
525    fi
526
527    if ( ${ExitFlag} ) ; then
528      # Inform the stack file
529      if [ X${ActivateStackFilling} = Xtrue ] ; then
530        echo '!!! ExitFlag has been activated !!!' >> ${StackFileLocation}/${StackFileName}
531      fi
532
533      # Unplugged message 4900 handling for now. To ease downstream treatment.
534      if [ X${ActivateBigBro} = Xtrue ] ; then
535        if [ X${TaskType} = Xcomputing ]; then
536          # RabbitMQ message code "COMPUTING JOBs COMMAND FAILURE"
537          code=1900
538        elif [ X${TaskType} = Xpost-processing ]; then
539          # RabbitMQ message code "POST-PROCESSING JOBs COMMAND FAILURE"
540          code=2900
541        elif [ X${TaskType} = Xchecking ]; then
542          # RabbitMQ message code "POST-PROCESSING FROM CHECKER JOBs COMMAND FAILURE"
543          code=3900
544        fi
545        # RabbitMQ message body
546        Body=$( echo "{${genericSimulationID},\"msgCode\":\"${code}\",\"msgUID\":\"$(uuidgen)\",\"command\":\"${command}\",\"msgTimestamp\":\"$( date +"%Y-%m-%dT%H:%M:%S.%N%z" )\"}" )
547
548        # Fill the rabbitMQ queue
549        IGCM_debug_sendAMQP
550      fi
551    else
552      # Inform the stack file
553      if [ X${ActivateStackFilling} = Xtrue ] ; then
554        echo "< ${IGCM_debug_LenStack} : ${@}" >> ${StackFileLocation}/${StackFileName}
555      fi
556    fi
557
558    # Reset array if necessary
559    if [ ${IGCM_debug_LenStack} = 0 ]; then
560      #echo
561      #IGCM_debug_Print 3 "Clean stack array"
562      #echo
563      unset IGCM_debug_Stack
564      unset IGCM_debug_StackArgs
565      unset IGCM_debug_StackTiming
566      IGCM_debug_Stack[0]=${NULL_STR}
567      IGCM_debug_StackArgs[0]=${NULL_STR}
568      IGCM_debug_StackTiming[0]=${NULL_STR}
569    fi
570  fi
571  #IGCM_debug_CallStack
572}
573
574#D-#==================================================================
575#D-function IGCM_debug_BigBro_Initialize
576#D-* Purpose: switch rabbitMQ on
577#D-
578function IGCM_debug_BigBro_Initialize {
579  IGCM_debug_PushStack "IGCM_debug_BigBro_Initialize"
580
581  typeset postProcessingIDLength postProcessingName postProcessingDate postProcessingDimn postProcessingComp postProcessingFile
582
583# Message type standard fields:
584# https://github.com/Prodiguer/prodiguer-docs/wiki/MQ-Standard-Message-Fields
585
586# Message type dictionnary and custom fields:
587# https://github.com/Prodiguer/prodiguer-docs/wiki/Monitoring-Message-Dictionary
588
589  if [ X${BigBrother} = Xtrue ] ; then
590    # create a unique ID for this specific job
591    jobuid=$(uuidgen)
592
593    # get the assigned id by the scheduler for that job
594    IGCM_sys_getJobSchedulerID jobSchedulerID
595
596    if [ X${TaskType} = Xcomputing ]; then
597      if ( ${FirstInitialize} ) ; then
598        # RabbitMQ message code "BEGIN A SIMULATION"
599        code=0000
600        # create and persist a unique id for this simulation
601        simuid=$(uuidgen)
602        IGCM_card_WriteOption ${SUBMIT_DIR}/run.card Configuration simuid ${simuid}
603        # Standard fields for the first message
604        genericSimulationID=$( echo "\"msgApplication\":\"monitoring\",\"msgProducer\":\"libigcm\",\"msgProducerVersion\":\"${libIGCMVersion}\",\"activity\":\"IPSL\",\"name\":\"${config_UserChoices_JobName}\",\"experiment\":\"${config_UserChoices_ExperimentName}\",\"space\":\"${config_UserChoices_SpaceName}\",\"model\":\"${config_UserChoices_TagName}\",\"startDate\":\"${config_UserChoices_DateBegin}\",\"endDate\":\"${config_UserChoices_DateEnd}\",\"login\":\"${LOGIN}\",\"centre\":\"${CENTER}\",\"machine\":\"${MASTER}\",\"simuid\":\"${simuid}\",\"jobuid\":\"${jobuid}\"" )
605        # RabbitMQ message body with specific fields associated message codes treated here
606        Body=$( echo "{${genericSimulationID},\"msgCode\":\"${code}\",\"accountingProject\":\"${PROJECT}\",\"jobWarningDelay\":\"${jobWarningDelay}\",\"msgUID\":\"$(uuidgen)\",\"msgTimestamp\":\"$( date +"%Y-%m-%dT%H:%M:%S.%N%z" )\"}" )
607        # Fill the rabbitMQ queue (the config.card in use will be sent)
608        IGCM_debug_sendAMQP activate
609      else
610        # RabbitMQ message code "A NEW COMPUTING JOB IS RUNNING PART OF A SIMULATION"
611        code=1000
612        # retrieve this simulation's unique id
613        IGCM_card_DefineVariableFromOption ${SUBMIT_DIR}/run.card Configuration simuid
614        simuid=${run_Configuration_simuid}
615        # Using standard fields for message others than the first one. Still subject to change
616        genericSimulationID=$( echo "\"msgApplication\":\"monitoring\",\"msgProducer\":\"libigcm\",\"msgProducerVersion\":\"${libIGCMVersion}\",\"simuid\":\"${simuid}\",\"jobuid\":\"${jobuid}\"" )
617        # RabbitMQ message body with specific fields associated message codes treated here
618        Body=$( echo "{${genericSimulationID},\"msgCode\":\"${code}\",\"accountingProject\":\"${PROJECT}\",\"jobWarningDelay\":\"${jobWarningDelay}\",\"jobSchedulerID\":\"${jobSchedulerID}\",\"jobSubmissionPath\":\"${SUBMIT_DIR}\",\"msgUID\":\"$(uuidgen)\",\"msgTimestamp\":\"$( date +"%Y-%m-%dT%H:%M:%S.%N%z" )\"}" )
619        # Fill the rabbitMQ queue
620        IGCM_debug_sendAMQP
621      fi
622
623      # NOT VERY NICE BUT ... IT WORKS
624      # Be sure that the genericSimulationID will be small from now on
625      # Using standard fields for messages others than the first one. Still subject to change
626      genericSimulationID=$( echo "\"msgApplication\":\"monitoring\",\"msgProducer\":\"libigcm\",\"msgProducerVersion\":\"${libIGCMVersion}\",\"simuid\":\"${simuid}\",\"jobuid\":\"${jobuid}\"" )
627
628    elif [ X${TaskType} = Xpost-processing ]; then
629      # RabbitMQ message code "A NEW POST-PROCESSING JOB IS RUNNING PART OF A SIMULATION"
630      code=2000
631      # retrieve this simulation's unique id
632      IGCM_card_DefineVariableFromOption ${SUBMIT_DIR}/run.card Configuration simuid
633      simuid=${run_Configuration_simuid}
634      # Using standard fields for message others than the first one. Still subject to change
635      genericSimulationID=$( echo "\"msgApplication\":\"monitoring\",\"msgProducer\":\"libigcm\",\"msgProducerVersion\":\"${libIGCMVersion}\",\"simuid\":\"${simuid}\",\"jobuid\":\"${jobuid}\"" )
636     
637      # Specify the post-processing task we are dealing with
638      postProcessingIDLength=$( echo "${Script_Post_Output}" | tr -d -c "\." | wc -c )
639      postProcessingName=$( echo "${Script_Post_Output}" | gawk -F. '{print $1}' )
640      postProcessingDate=$( echo "${Script_Post_Output}" | gawk -F. '{print $2}' )
641      postProcessingDimn="null"
642      postProcessingComp="null"
643      postProcessingFile="null"
644      if [ ${postProcessingIDLength} -eq 2 ] ; then
645        postProcessingDimn=$( echo "${Script_Post_Output}" | gawk -F. '{print $3}' )
646      elif [ ${postProcessingIDLength} -eq 4 ] ; then
647        postProcessingComp=$( echo "${Script_Post_Output}" | gawk -F. '{print $4}' )
648        postProcessingFile=$( echo "${Script_Post_Output}" | gawk -F. '{print $5}' )
649      fi
650
651      # RabbitMQ message body with specific fields associated message codes treated here
652      Body=$( echo "{${genericSimulationID},\"msgCode\":\"${code}\",\"accountingProject\":\"${PROJECT}\",\"jobWarningDelay\":\"${jobWarningDelay}\",\"jobSchedulerID\":\"${jobSchedulerID}\",\"jobSubmissionPath\":\"${SUBMIT_DIR}\",\"msgUID\":\"$(uuidgen)\",\"msgTimestamp\":\"$( date +"%Y-%m-%dT%H:%M:%S.%N%z" )\",\"postProcessingName\":\"${postProcessingName}\",\"postProcessingDate\":\"${postProcessingDate}\",\"postProcessingDimn\":\"${postProcessingDimn}\",\"postProcessingComp\":\"${postProcessingComp}\",\"postProcessingFile\":\"${postProcessingFile}\"}" )
653      # Fill the rabbitMQ queue
654      IGCM_debug_sendAMQP
655    fi
656    # Turn the flag on
657    ActivateBigBro=true
658    # Dont flush by default
659    FlushAMQP=false
660  fi
661  IGCM_debug_PopStack "IGCM_debug_BigBro_Initialize"
662}
663
664#D-#==================================================================
665#D-function IGCM_debug_BigBro_Finalize
666#D-* Purpose: Finalize rabbitMQ messages exchanges
667#D-
668function IGCM_debug_BigBro_Finalize {
669  IGCM_debug_PushStack "IGCM_debug_BigBro_Finalize"
670
671  # Message type standard fields:
672  # https://github.com/Prodiguer/prodiguer-docs/wiki/MQ-Standard-Message-Fields
673
674  # Message type dictionnary and custom fields:
675  # https://github.com/Prodiguer/prodiguer-docs/wiki/Monitoring-Message-Dictionary
676
677  if ( $DEBUG_debug ) ; then
678    if [ X${ActivateBigBro} = Xtrue ] ; then
679      if [ X${TaskType} = Xcomputing ]; then
680        if ( ${simulationIsOver} ) ; then
681          # RabbitMQ message code "SIMULATION ENDS"
682          code=0100
683          FlushAMQP=true
684        elif ( ${ExitFlag} ) ; then
685          # RabbitMQ message code "EXIT THE JOBS BECAUSE ERROR(S) HAS BEEN TRIGGERED"
686          code=1999
687          FlushAMQP=true
688        else
689          # RabbitMQ message code "COMPUTING JOB ENDS"
690          code=1100
691        fi
692      elif [ X${TaskType} = Xpost-processing ]; then
693        if ( ${ExitFlag} ) ; then
694          # RabbitMQ message code "POST-PROCESSING JOB FAILS"
695          code=2999
696          FlushAMQP=true
697        else
698          # RabbitMQ message code "POST-PROCESSING JOB ENDS"
699          code=2100
700          FlushAMQP=true
701        fi
702      elif [ X${TaskType} = Xchecking ]; then
703        if ( ${ExitFlag} ) ; then
704          # RabbitMQ message code "POST-PROCESSING JOB FAILS"
705          code=3999
706          FlushAMQP=true
707        else
708          # RabbitMQ message code "POST-PROCESSING JOB ENDS"
709          code=3100
710          FlushAMQP=true
711        fi
712      fi
713      # RabbitMQ message body
714      Body=$( echo "{${genericSimulationID},\"msgCode\":\"${code}\",\"msgUID\":\"$(uuidgen)\",\"msgTimestamp\":\"$( date +"%Y-%m-%dT%H:%M:%S.%N%z" )\"}" )
715      # Fill the rabbitMQ queue
716      IGCM_debug_sendAMQP
717    fi
718  fi
719 
720  IGCM_debug_PopStack "IGCM_debug_BigBro_Finalize"
721}
722
723#D-#==================================================================
724#D-function IGCM_debug_Exit
725#D-* Purpose: Print Call Stack and set ExitFlag to true
726#D-
727function IGCM_debug_Exit {
728  IGCM_debug_PushStack "IGCM_debug_Exit"
729  echo "IGCM_debug_Exit : " "${@}"
730  echo
731  echo "!!!!!!!!!!!!!!!!!!!!!!!!!!"
732  echo "!!   ERROR TRIGGERED    !!"
733  echo "!!   EXIT FLAG SET      !!"
734  echo "!------------------------!"
735  echo
736  IGCM_debug_CallStack
737  ExitFlag=true
738  IGCM_debug_PopStack "IGCM_debug_Exit"
739}
740
741#D-#==================================================
742#D-function IGCM_debug_Verif_Exit
743#D-* Purpose: exit with number 1 if ExitFlag is true
744#D-
745function IGCM_debug_Verif_Exit {
746  if ( ${ExitFlag} ) ; then
747    echo "IGCM_debug_Verif_Exit : Something wrong happened previously."
748    echo "IGCM_debug_Verif_Exit : ERROR and EXIT keyword will help find out where."
749    # Only computing TaskType stops the job for now.
750    if [ X${TaskType} = Xcomputing ] ; then
751      IGCM_card_WriteOption ${SUBMIT_DIR}/run.card Configuration PeriodState "Fatal"
752      echo "                        EXIT THE JOB."
753      echo
754      IGCM_debug_CallStack
755
756      # Mail notification
757      IGCM_sys_SendMail
758
759      # Inform the rabbitMQ queue
760      IGCM_debug_BigBro_Finalize
761
762      # And Good Bye
763      date
764      exit 1
765
766    elif [ X${TaskType} = Xpost-processing ] ; then
767      # If SpaceName is PROD then we stop when post_processing failed
768      if [ X${config_UserChoices_SpaceName} = XPROD ] ; then
769        echo "                        EXIT THE POST-PROCESSING JOB."
770        echo
771        IGCM_debug_CallStack
772
773        # Notify the computing job that something wrong happened.
774        IGCM_card_WriteOption ${SUBMIT_DIR}/run.card Configuration PeriodState "Fatal"
775
776        # Mail notification?
777        #IGCM_sys_SendMailPost
778
779        # Inform the rabbitMQ queue
780        IGCM_debug_BigBro_Finalize
781
782        # And Good Bye
783        date
784        exit 1
785      else
786        echo "In config.card the variable SpaceName is not in PROD"
787        echo "              SO WE DO NOT EXIT THE JOB."
788        echo
789
790        # Inform the rabbitMQ queue
791        IGCM_debug_BigBro_Finalize
792
793        date
794      fi
795    elif [ X${TaskType} = Xchecking ] ; then
796      echo "Nothing will happen for now"
797    fi
798  fi
799}
800
801#D-#==================================================================
802#D-function IGCM_debug_Print
803#D-* Purpose: Print arguments according to a level of verbosity.
804#D-
805function IGCM_debug_Print
806{
807  typeset level=$1
808  shift
809
810  if [ X"${1}" = X"-e" ]; then
811    typeset cmd_echo="echo -e"
812    shift
813  else
814    typeset cmd_echo="echo"
815  fi
816
817  if [ ${level} -le ${Verbosity} ] ; then
818    typeset i
819    case "${level}" in
820    1) for i in "$@" ; do
821      ${cmd_echo} $(date +"%Y-%m-%d %T") "--Debug1-->" ${i}
822      done ;;
823    2) for i in "$@" ; do
824      ${cmd_echo} $(date +"%Y-%m-%d %T") "--------Debug2-->" ${i}
825      done ;;
826    3) for i in "$@" ; do
827      ${cmd_echo} $(date +"%Y-%m-%d %T") "--------------Debug3-->" ${i}
828      done ;;
829    esac
830  fi
831}
832
833#D-#==================================================================
834#D-function IGCM_debug_PrintVariables
835#D-* Purpose: Print arguments when match a pattern
836#D-           according to a level of verbosity.
837function IGCM_debug_PrintVariables
838{
839  typeset level=$1
840  shift
841
842  list=$( set | grep ^$1 | sed -e "s/'//g" )
843
844  if [ "X${list}" != X ]  ; then
845    IGCM_debug_Print ${level} ${list}
846  fi
847}
848
849#D-#==================================================================
850#D-function IGCM_debug_PrintInfosActions
851#D-* Purpose: Print information related to instrumentation
852function IGCM_debug_PrintInfosActions
853{
854  typeset actionType=$1
855  typeset entitySize=$2
856  typeset start_ms=$3
857  typeset end_ms=$4
858
859  typeset dest=$5
860  typeset source=$6
861
862  typeset diff_ms entitySizeKo entitySizeMo flux_Ko_ms flux_Ko_s flux_Mo_s
863  typeset dirFrom dirTo
864
865  diff_ms=$(( $end_ms - $start_ms ))
866  # echo "diff_ms=$diff_ms"
867
868  entitySizeKo=$( echo ${entitySize} | gawk -F"|" '{print $1}' )
869  # echo "entitySizeKo=$entitySizeKo"
870  entitySizeMo=$( echo ${entitySize} | gawk -F"|" '{print $2}' )
871
872  # flux en Ko / ms
873  flux_Ko_ms=$( echo "scale=6;${entitySizeKo}/${diff_ms}" | bc )
874  # echo "flux_Ko_ms=$flux_Ko_ms"
875
876  # flux en Ko / s
877  flux_Ko_s=$(( $flux_Ko_ms * 1000 ))
878  # echo "flux_Ko_s=$flux_Ko_s"
879
880  # flux en Mo / s
881  flux_Mo_s=$( echo "scale=6;${flux_Ko_s}/1024" | bc )
882  # echo "flux_Mo_s=$flux_Mo_s"
883
884  if [ -d $dest ] ; then
885    dirTo=$( readlink -f ${dest} )
886  else
887    dirTo=$( readlink -f $( dirname ${dest} ) )
888  fi
889
890  if [ -d $source ] ; then
891    dirFrom=$( readlink -f ${source} )
892  else
893    dirFrom=$( readlink -f $( dirname ${source} ) )
894  fi
895
896  instrumentationContent=$( echo "\"actionName\":\"${actionType}\",\"size_Mo\":\"${entitySizeMo}\",\"duration_ms\":\"${diff_ms}\",\"throughput_Mo_s\":\"${flux_Mo_s}\",\"dirFrom\":\"${dirFrom}\",\"dirTo\":\"${dirTo}\"" )
897
898  if [ X${ActivateStackFilling} = Xtrue ] ; then
899    echo "{${instrumentationContent}}" >> ${StackFileLocation}/${StackFileName}
900  fi
901
902  # Inform the rabbitMQ queue
903  if [ X${ActivateBigBro} = Xtrue ] ; then
904    # RabbitMQ message body
905    Body=$( echo "{${genericSimulationID},\"msgCode\":\"7000\",\"msgUID\":\"$(uuidgen)\",${instrumentationContent},\"msgTimestamp\":\"$( date +"%Y-%m-%dT%H:%M:%S.%N%z" )\"}" )
906    # Fill the rabbitMQ queue
907    IGCM_debug_sendAMQP
908  fi
909}
910
911#D-#==================================================================
912#D-function IGCM_debug_Check
913#D- * Purpose: Check the present file by comparison with a reference file
914function IGCM_debug_Check
915{
916  #---------------------
917  if [ ! -n "${libIGCM}" ] ; then
918    echo "Check libIGCM_debug ..........................................[ FAILED ]"
919    echo "--Error--> libIGCM variable is not defined"
920    exit 2
921  fi
922
923  #---------------------
924  if [ ! -n "${Verbosity}" ] ; then
925    echo "Check libIGCM_debug ..........................................[ FAILED ]"
926    echo "--Error--> Verbosity variable is not defined"
927    exit 3
928  fi
929
930  #---------------------
931  # Need to remove timestamps here
932  diff ${libIGCM}/libIGCM_debug/IGCM_debug_Test.ref <(${libIGCM}/libIGCM_debug/IGCM_debug_Test.ksh | sed -e "s:[0-9][0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9] [0-9][0-9]\:[0-9][0-9]\:[0-9][0-9] ::g") > /dev/null 2>&1
933  status=$?
934
935  if [ ${status} -eq 0 ] ; then
936    echo "Check libIGCM_debug ..............................................[ OK ]"
937  else
938    echo "Check libIGCM_debug ..........................................[ FAILED ]"
939    echo "--Error--> Execution of ${libIGCM}/libIGCM_debug/IGCM_debug_Test.ksh"
940    echo "           has produced the file IGCM_debug_Test.ref.failed"
941    echo "           Please analyse differences with the reference file by typing:"
942    echo "           diff IGCM_debug_Test.ref.failed ${libIGCM}/libIGCM_debug/IGCM_debug_Test.ref"
943    echo "           Report errors to the author: Patrick.Brockmann@cea.fr"
944    diff ${libIGCM}/libIGCM_debug/IGCM_debug_Test.ref <(${libIGCM}/libIGCM_debug/IGCM_debug_Test.ksh | sed -e "s:[0-9][0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9] [0-9][0-9]\:[0-9][0-9]\:[0-9][0-9] ::g")
945    exit 4
946  fi
947  #---------------------
948}
Note: See TracBrowser for help on using the repository browser.