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

Last change on this file since 855 was 855, checked in by sdipsl, 11 years ago

First try. Include rabbitMQ C client call within IGCM_debug_PushStack and IGCM_debug_PopStack. See r138
Flag protected so won't affect normal behaviour

  • 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: 15.2 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
27typeset DEBUG_debug=${DEBUG_debug:=false}
28
29# Where the stack file containing call tree will be stored.
30typeset StackFileLocation=${StackFileLocation:=${PWD}}
31
32if ( $DEBUG_debug ) ; then
33  if [ -f ${StackFileLocation}/stack ] ;
34  then
35    echo "Stack of an libIGCM job :" >> ${StackFileLocation}/stack
36  else
37    echo "Stack of an libIGCM job :" >  ${StackFileLocation}/stack
38  fi
39
40  if [ -f ${StackFileLocation}/fullstack ] ;
41  then
42    echo "Full stack of an libIGCM job :" >> ${StackFileLocation}/fullstack
43  else
44    echo "Full stack of an libIGCM job :" >  ${StackFileLocation}/fullstack
45  fi
46
47fi
48
49#==================================================
50# NULL_STR
51# Default null string
52typeset -r NULL_STR="_0_" 
53
54#==================================================
55# libIGCM_CurrentTag
56# Current libIGCM tag, check compatibilty with *.card
57typeset -r libIGCM_CurrentTag="1.0" 
58
59#==================================================
60# Exit Flag (internal debug)
61# When true, end the master loop AFTER SAVES FILES
62ExitFlag=false
63
64#==================================================
65# Declare a stack of functions calls
66
67# insert last argument of the Stack
68#set -A IGCM_debug_Stack ${NULL_STR}
69#set -A IGCM_debug_StackArgs ${NULL_STR}
70unset IGCM_debug_Stack
71unset IGCM_debug_StackArgs
72IGCM_debug_Stack[0]=${NULL_STR}
73IGCM_debug_StackArgs[0]=${NULL_STR}
74IGCM_debug_LenStack=0
75
76#D-#==================================================================
77#D-function IGCM_debug_CallStack
78#D-* Purpose: Echo the Stack
79#D-
80function IGCM_debug_CallStack {
81  #echo
82  #echo "!!!!!!!!!!!!!!!!!!!!!!!!!!"
83  #echo "!! IGCM_debug_CallStack !!"
84  #echo "!------------------------!"
85  #echo
86  if ( $DEBUG_debug ) ; then
87    # La pile d'appels est affichée de la plus vieille à la plus récente
88    # (c'est donc l'inverse de la norme d'affichage).
89    typeset i decal
90    i=0
91    until [ $i -eq ${IGCM_debug_LenStack} ]; do
92      decal=0
93      until [ $decal -eq ${i} ]; do
94        printf -- ' ' >> ${StackFileLocation}/fullstack
95        (( decal = decal + 1 ))
96      done
97      echo "$i - ${IGCM_debug_Stack[$(( $IGCM_debug_LenStack-$i-1 ))]}"\
98           "(${IGCM_debug_StackArgs[$(( $IGCM_debug_LenStack-$i-1 ))]})" >> ${StackFileLocation}/fullstack
99      ((i = i + 1))
100    done
101    #echo "!------------------------!"
102  fi
103}
104
105#D-#==================================================================
106#D-function IGCM_debug_PushStack
107#D-* Purpose: Push a function name in the stack
108#D-
109function IGCM_debug_PushStack {
110
111  if ( $DEBUG_debug ) ; then
112    typeset decal inputs
113    echo >> ${StackFileLocation}/stack
114    decal=0
115    while [ ${decal} -lt ${IGCM_debug_LenStack} ]; do
116      printf ' ' >> ${StackFileLocation}/stack
117      (( decal = decal + 1 ))
118    done
119
120    # STORE input list in an indexed array
121    INPUTS=( $@ )
122    # We add function call name on beginning of the stack
123    set +A IGCM_debug_Stack -- ${1} ${IGCM_debug_Stack[*]}
124
125    # We include the "null" Args in the beginning of the StackArgs
126    set +A IGCM_debug_StackArgs ${NULL_STR} ${IGCM_debug_StackArgs[*]} 
127    # Then, we shift StackArgs tabular
128    if [ $# -gt 1 ]; then
129      IGCM_debug_StackArgs[0]=$(echo ${INPUTS[*]:1} | sed -e "s/\ /,/g")
130    fi
131
132    # Fill the stack file
133    echo "> ${IGCM_debug_LenStack} : ${@}" >> ${StackFileLocation}/stack
134
135    # Fill the rabbitMQ queue
136    if [ X${ActivateBigBro} = Xtrue ] ; then
137      code=2000
138      #
139      encodedBody=$( echo "{\"code\":\"${code}\",\"jobid\":\"${jobid}\",\"nesting\":\"${IGCM_debug_LenStack}\",\"command\":\"${INPUTS[*]}\",\"timestamp\":\"$( date +"%Y-%m-%d-%T" )\"}" | base64 -w 0 )
140      #
141      #sendAMQPMsg -h localhost -p 5672 -f ${SUBMIT_DIR}/config.card.base64 -b ${encodedBody}
142      echo sendAMQPMsg -h localhost -p 5672 -b ${encodedBody} >> /tmp/sendAMQP.history.txt
143      sendAMQPMsg -h localhost -p 5672 -b ${encodedBody}
144      status=$?
145      if [ ${status} -gt 0 ] ; then
146        IGCM_debug_Print 2 "IGCM_debug_PushStack : command sendAMQPMsg failed error code ${status}"
147        echo  sendAMQPMsg -h localhost -p 5672 -b ${encodedBody}
148        exit
149      fi
150    fi
151
152    # Increment LenStack
153    (( IGCM_debug_LenStack = IGCM_debug_LenStack + 1 ))
154
155    # If you want to print CallStack each time :
156    IGCM_debug_CallStack
157  fi
158}
159
160#D-#==================================================================
161#D-function IGCM_debug_PopStack
162#D-* Purpose: Pop a function name in the stack
163#D-
164function IGCM_debug_PopStack {
165  if ( $DEBUG_debug ) ; then
166
167    typeset decal
168    if [ "${IGCM_debug_Stack[0]}" = "${1}" ]; then
169      (( IGCM_debug_LenStack = IGCM_debug_LenStack - 1 ))
170      set -A IGCM_debug_Stack -- ${IGCM_debug_Stack[*]:1}
171      set -A IGCM_debug_StackArgs -- ${IGCM_debug_StackArgs[*]:1}
172    else
173      echo 'IGCM_debug_Exit : stack is corrupted ! LenStack =' ${IGCM_debug_LenStack}
174      IGCM_debug_Exit $@
175    fi
176    decal=0
177    while [ ${decal} -lt ${IGCM_debug_LenStack} ]; do
178      printf ' ' >> ${StackFileLocation}/stack
179      (( decal = decal + 1 ))
180    done
181
182    if ( ${ExitFlag} ) ; then
183      # Inform the stack file
184      echo '!!! ExitFlag has been activated !!!' >> ${StackFileLocation}/stack
185      # Inform the rabbitMQ queue
186      if [ X${ActivateBigBro} = Xtrue ] ; then
187        code=9000
188        #
189        encodedBody=$( echo "{\"code\":\"${code}\",\"jobid\":\"${jobid}\",\"status\":\"NOK\",\"out\":\"true\",\"nesting\":\"${IGCM_debug_LenStack}\",\"command\":\"${INPUTS[*]}\",\"timestamp\":\"$( date +"%Y-%m-%d-%T" )\"}" | base64 -w 0 )
190        #
191        #sendAMQPMsg -h localhost -p 5672 -f ${SUBMIT_DIR}/config.card -b ${encodedBody}
192        echo sendAMQPMsg -h localhost -p 5672 -b ${encodedBody} >> /tmp/sendAMQP.history.txt
193        sendAMQPMsg -h localhost -p 5672 -b ${encodedBody}
194        status=$?
195        if [ ${status} -gt 0 ] ; then
196          IGCM_debug_Print 2 "IGCM_debug_PopStack : command sendAMQPMsg failed error code ${status}"
197          echo  sendAMQPMsg -h localhost -p 5672 -b ${encodedBody}
198          exit
199        fi
200      fi
201    else
202      # Inform the stack file
203      echo "< ${IGCM_debug_LenStack} : ${@}" >> ${StackFileLocation}/stack
204
205      # Inform the rabbitMQ queue
206      if [ X${ActivateBigBro} = Xtrue ] ; then
207        code=3000
208        #
209        encodedBody=$( echo "{\"code\":\"${code}\",\"jobid\":\"${jobid}\",\"status\":\"OK\",\"out\":\"true\",\"nesting\":\"${IGCM_debug_LenStack}\",\"command\":\"${INPUTS[*]}\",\"timestamp\":\"$( date +"%Y-%m-%d-%T" )\"}" | base64 -w 0 )
210
211        #
212        #sendAMQPMsg -h localhost -p 5672 -f ${SUBMIT_DIR}/config.card.base64 -b ${encodedBody}
213        echo sendAMQPMsg -h localhost -p 5672 -b ${encodedBody} >> /tmp/sendAMQP.history.txt
214        sendAMQPMsg -h localhost -p 5672 -b ${encodedBody}
215        status=$?
216        if [ ${status} -gt 0 ] ; then
217          IGCM_debug_Print 2 "IGCM_debug_PopStack : command sendAMQPMsg failed error code ${status}"
218          echo  sendAMQPMsg -h localhost -p 5672 -b ${encodedBody}
219          exit
220        fi
221      fi
222    fi
223
224    if [ ${IGCM_debug_LenStack} = 0 ]; then
225      # Reset array only when necessary
226      #echo
227      #IGCM_debug_Print 3 "Clean stack array"
228      #echo
229      #set -A IGCM_debug_Stack ${NULL_STR}
230      #set -A IGCM_debug_StackArgs ${NULL_STR}
231      unset IGCM_debug_Stack
232      unset IGCM_debug_StackArgs
233      IGCM_debug_Stack[0]=${NULL_STR}
234      IGCM_debug_StackArgs[0]=${NULL_STR}
235    fi
236  fi
237  IGCM_debug_CallStack
238}
239
240#D-#==================================================================
241#D-function IGCM_debug_ActivateBigBro
242#D-* Purpose: switch rabbitMQ on
243#D-
244function IGCM_debug_ActivateBigBro {
245  IGCM_debug_PushStack "IGCM_debug_ActivateBigBro"
246
247  set -vx
248
249  # Fill the rabbitMQ queue
250  if [ X${BigBrother}=Xtrue ] ; then
251    jobid=${config_UserChoices_JobName}.${config_UserChoices_ExperimentName}.${config_UserChoices_SpaceName}.${config_UserChoices_TagName}.p86denv.TGCC.CURIE
252    if ( ${FirstInitialize} ) ; then
253      code=0000
254    else
255      code=1000
256    fi
257    encodedBody=$( echo "{\"code\":\"${code}\",\"jobid\":\"${jobid}\",\"status\":\"OK\",\"out\":\"false\",\"nesting\":\"${IGCM_debug_LenStack}\",\"timestamp\":\"$( date +"%Y-%m-%d-%T" )\"}" | base64 -w 0 )
258    #
259    cat ${SUBMIT_DIR}/config.card | base64 -w 0 > ${SUBMIT_DIR}/config.card.base64
260    #
261    #sendAMQPMsg -h localhost -p 5672 -f ${SUBMIT_DIR}/config.card.base64 -b ${encodedBody}
262    echo sendAMQPMsg -h localhost -p 5672 -f ${SUBMIT_DIR}/config.card.base64 -b ${encodedBody} >> /tmp/sendAMQP.history.txt
263    sendAMQPMsg -h localhost -p 5672 -f ${SUBMIT_DIR}/config.card.base64 -b ${encodedBody}
264    status=$?
265    if [ ${status} -gt 0 ] ; then
266      IGCM_debug_Print 2 "IGCM_debug_ActivateBigBro : command failed error code ${status}"
267      IGCM_debug_Exit "IGCM_debug_ActivateBigBro"
268    fi
269    ActivateBigBro=true
270  fi
271  IGCM_debug_PopStack "IGCM_debug_ActivateBigBro"
272}
273
274#D-#==================================================================
275#D-function IGCM_debug_Exit
276#D-* Purpose: Print Call Stack and set ExitFlag to true
277#D-
278function IGCM_debug_Exit {
279  IGCM_debug_PushStack "IGCM_debug_Exit"
280  echo "IGCM_debug_Exit : " "${@}"
281  #IGCM_debug_CallStack
282  ExitFlag=true
283  IGCM_debug_PopStack "IGCM_debug_Exit"
284}
285
286#D-#==================================================
287#D-function IGCM_debug_Verif_Exit
288#D-* Purpose: exit with number 1 if ExitFlag is true
289#D-
290function IGCM_debug_Verif_Exit {
291  if ( ${ExitFlag} ) ; then
292    # Plan to send an email here with IGCM_sys_SendMail
293    if [ X${TaskType} != Xchecking ] ; then
294      IGCM_card_WriteOption ${SUBMIT_DIR}/run.card Configuration PeriodState "Fatal"
295      echo "IGCM_debug_Verif_Exit : Something wrong happened."
296      echo "                        EXIT THE JOB."
297      echo
298    fi
299    if ( $DEBUG_debug ) ; then
300      echo "Your files on ${R_OUT} :"
301      IGCM_sys_Tree ${R_SAVE}
302      echo
303    fi
304    if [ X${ActivateBigBro} = Xtrue ] ; then
305      code=9999
306      #
307      encodedBody=$( echo "{\"code\":\"${code}\",\"jobid\":\"${jobid}\",\"status\":\"FATAL\",\"timestamp\":\"$( date +"%Y-%m-%d-%T" )\"}" | base64 -w 0 )
308      #
309      #sendAMQPMsg -h localhost -p 5672 -f ${SUBMIT_DIR}/config.card -b ${encodedBody}
310      echo sendAMQPMsg -h localhost -p 5672 -b ${encodedBody} >> /tmp/sendAMQP.history.txt
311      sendAMQPMsg -h localhost -p 5672 -b ${encodedBody}
312      status=$?
313      if [ ${status} -gt 0 ] ; then
314        IGCM_debug_Print 2 "IGCM_debug_PopStack : command sendAMQPMsg failed error code ${status}"
315        echo  sendAMQPMsg -h localhost -p 5672 -b ${encodedBody}
316        exit
317      fi
318    fi
319    # Mail notification
320    IGCM_sys_SendMail
321    # And Good Bye
322    date
323    exit 1
324  fi
325}
326
327#D-#==================================================
328#D-function IGCM_debug_Verif_Exit_Post
329#D-* Purpose: exit with number 1 if ExitFlag is true for Post-treatment
330#D-
331function IGCM_debug_Verif_Exit_Post {
332  if ( ${ExitFlag} ) ; then
333    echo "IGCM_debug_Verif_Exit_Post : Something wrong happened."
334    # If SpaceName is PROD then we stop if post_processing fails
335    # Plan to send an email here with IGCM_sys_SendMail
336    if [ X${config_UserChoices_SpaceName} = XPROD ] ; then
337      echo "                        EXIT THE JOB."
338      echo
339      # Mail notification
340      #IGCM_sys_SendMailPost
341      # And Good Bye
342      date
343      exit 1
344    else
345      echo "Either inside config.card the variable SpaceName is not in PROD"
346      echo "or inside the main Job the variable JobType is not in RUN mode"
347      echo "              SO WE DO NOT EXIT THE JOB."
348      echo
349      date
350    fi
351  fi
352}
353
354#D-#==================================================================
355#D-function IGCM_debug_Print
356#D-* Purpose: Print arguments according to a level of verbosity.
357#D-
358function IGCM_debug_Print
359{
360  typeset level=$1
361  shift
362
363  if [ X"${1}" = X"-e" ]; then
364    typeset cmd_echo="echo -e"
365    shift
366  else
367    typeset cmd_echo="echo"
368  fi
369
370  if [ ${level} -le ${Verbosity} ] ; then
371    typeset i
372    case "${level}" in
373    1) for i in "$@" ; do
374      ${cmd_echo} $(date +"%Y-%m-%d %T") "--Debug1-->" ${i}
375      done ;;
376    2) for i in "$@" ; do
377      ${cmd_echo} $(date +"%Y-%m-%d %T") "--------Debug2-->" ${i}
378      done ;;
379    3) for i in "$@" ; do
380      ${cmd_echo} $(date +"%Y-%m-%d %T") "--------------Debug3-->" ${i}
381      done ;;
382    esac
383  fi
384}
385
386#D-#==================================================================
387#D-function IGCM_debug_PrintVariables
388#D-* Purpose: Print arguments when match a pattern
389#D-           according to a level of verbosity.
390function IGCM_debug_PrintVariables
391{
392  typeset level=$1
393  shift
394
395  list=$( set | grep ^$1 | sed -e "s/'//g" )
396
397  if [ "X${list}" != X ]  ; then
398    IGCM_debug_Print ${level} ${list}
399  fi
400}
401
402#D-#==================================================================
403#D-function IGCM_debug_Check
404#D- * Purpose: Check the present file by comparison with a reference file
405function IGCM_debug_Check
406{
407  #---------------------
408  if [ ! -n "${libIGCM}" ] ; then
409    echo "Check libIGCM_debug ..........................................[ FAILED ]"
410    echo "--Error--> libIGCM variable is not defined"
411    exit 2
412  fi
413
414  #---------------------
415  if [ ! -n "${Verbosity}" ] ; then
416    echo "Check libIGCM_debug ..........................................[ FAILED ]"
417    echo "--Error--> Verbosity variable is not defined"
418    exit 3
419  fi
420
421  #---------------------
422  ${libIGCM}/libIGCM_debug/IGCM_debug_Test.ksh > IGCM_debug_Test.ref.failed 2>&1
423  sleep 2
424
425  # Remove date stamp.
426  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" IGCM_debug_Test.ref.failed > IGCM_debug_Test.ref.failed.nodate
427  mv IGCM_debug_Test.ref.failed.nodate IGCM_debug_Test.ref.failed
428
429  if diff IGCM_debug_Test.ref.failed ${libIGCM}/libIGCM_debug/IGCM_debug_Test.ref > /dev/null 2>&1 ; then
430    echo "Check libIGCM_debug ..............................................[ OK ]"
431    rm -f IGCM_debug_Test.ref.failed
432  else
433    echo "Check libIGCM_debug ..........................................[ FAILED ]"
434    echo "--Error--> Execution of ${libIGCM}/libIGCM_debug/IGCM_debug_Test.ksh"
435    echo "           has produced the file IGCM_debug_Test.ref.failed"
436    echo "           Please analyse differences with the reference file by typing:"
437    echo "           diff IGCM_debug_Test.ref.failed ${libIGCM}/libIGCM_debug/IGCM_debug_Test.ref"
438    echo "           Report errors to the author: Patrick.Brockmann@cea.fr"
439    exit 4
440  fi
441  #---------------------
442}
Note: See TracBrowser for help on using the repository browser.