Octopus
debug.F90
Go to the documentation of this file.
1!! Copyright (C) 2016 X. Andrade
2!!
3!! This program is free software; you can redistribute it and/or modify
4!! it under the terms of the GNU General Public License as published by
5!! the Free Software Foundation; either version 2, or (at your option)
6!! any later version.
7!!
8!! This program is distributed in the hope that it will be useful,
9!! but WITHOUT ANY WARRANTY; without even the implied warranty of
10!! MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11!! GNU General Public License for more details.
12!!
13!! You should have received a copy of the GNU General Public License
14!! along with this program; if not, write to the Free Software
15!! Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
16!! 02110-1301, USA.
17!!
18
19#include "global.h"
20
21module debug_oct_m
22 use global_oct_m
24 use mpi_oct_m
26 use loct_oct_m
27 use parser_oct_m
28
29 implicit none
30
31 private
32 public :: &
33 debug_t, &
34 debug_init, &
39 debug, &
41#ifndef NDEBUG
44#endif
46
47 type debug_t
48 private
49 logical, public :: info
50 logical, public :: trace
51 logical, public :: trace_term
52 logical, public :: trace_file
53 logical :: extra_checks
54 logical, public :: interaction_graph
55 logical, public :: interaction_graph_full
56 logical, public :: propagation_graph
57 logical, public :: instrument
58 integer :: bits
59 character(len=MAX_PATH_LEN), public :: instr_sub_name
60 integer, public :: instr_tool
61 end type debug_t
62
63 type(debug_t), save :: debug
64
66 integer, parameter :: unit_offset = 1000
67
68 interface
69 subroutine debug_verrou_start_instrumentation() bind(C)
71
72 subroutine debug_verrou_stop_instrumentation() bind(C)
74
75 subroutine debug_fenv_start_instrumentation() bind(C)
77
78 subroutine debug_fenv_stop_instrumentation() bind(C)
80 end interface
81
82contains
83
84 subroutine debug_init(this, namespace)
85 type(debug_t), intent(out) :: this
86 type(namespace_t), intent(in) :: namespace
87
88 character(len=256) :: node_hook
89 logical :: file_exists, mpi_debug_hook
90 integer :: sec, usec
91 type(block_t) :: blk
92 integer :: line
93
94 !%Variable Debug
95 !%Type flag
96 !%Default no
97 !%Section Execution::Debug
98 !%Description
99 !% This variable controls the amount of debugging information
100 !% generated by Octopus. You can use include more than one option
101 !% with the + operator.
102 !%Option no 0
103 !% (default) <tt>Octopus</tt> does not enter debug mode.
104 !%Option info 1
105 !% Octopus prints additional information to the terminal.
106 !%Option trace 2
107 !% Octopus generates a stack trace as it enters end exits
108 !% subroutines. This information is reported if Octopus stops with
109 !% an error.
110 !%Option trace_term 4
111 !% The trace is printed to the terminal as Octopus enters or exits subroutines. This slows down execution considerably.
112 !%Option trace_file 8
113 !% The trace is written to files in the <tt>debug</tt>
114 !% directory. For each node (when running in parallel) there is a file called
115 !% <tt>debug_trace.&lt;rank&gt;</tt>. Writing these files slows down the code by a huge factor and
116 !% it is usually only necessary for parallel runs.
117 !%Option extra_checks 16
118 !% This enables Octopus to perform some extra checks, to ensure
119 !% code correctness, that might be too costly for regular runs.
120 !%Option interaction_graph 32
121 !% Octopus generates a dot file containing the graph for a multisystem run.
122 !%Option interaction_graph_full 64
123 !% Octopus generates a dot file containing the graph for a multisystem run including ghost interactions.
124 !%Option propagation_graph 128
125 !% Octopus generates a file with information for the propagation diagram.
126 !%Option instrument 256
127 !% Octopus adds instrumentation to functions specified in an <tt>InstrumentFunctions</tt> block.
128 !%End
129 call parse_variable(namespace, 'Debug', option__debug__no, this%bits)
130
131 call from_bits(this)
132
133 !%Variable InstrumentFunctions
134 !%Type block
135 !%Section Execution::Debug
136 !%Description
137 !% This input options controls which routines are going to be instrumented
138 !% for the tools selected using the <tt>Debug=instrument</tt> option.
139 !%
140 !% <br>%<tt>InstrumentFunctions
141 !% <br>&nbsp;&nbsp;"function_name" | instrumentation_tool
142 !% <br>%</tt>
143 !%
144 !% Here is an example to better understand how this works:
145 !%
146 !% <br>%<tt>InstrumentFunctions
147 !% <br>&nbsp;&nbsp;"grid/grid.F90.grid_init_from_grid_stage_1" | verrou
148 !% <br>%</tt>
149 !%
150 !% NOTE: Currently only a single function can be instrumented!
151 !%
152 !% Available instrumentation tools:
153 !%Option verrou 1
154 !% Verrou helps you look for floating-point round-off errors.
155 !%Option fenv 2
156 !% Enable floating-point exceptions. Requires Octopus to be compiled against glibc.
157 !%End
158 if (parse_block(namespace, "InstrumentFunctions", blk) == 0) then
159 ! TODO: Allow instrumentation of more than a single function
160 if (parse_block_n(blk) .gt. 1) then
161 write(stderr,'(a)') "Only single function can be instrumented!"
162 call mpi_world%abort()
163 call loct_exit_failure()
164 end if
166 do line = 0, parse_block_n(blk) - 1
167 call parse_block_string(blk, line, 0, this%instr_sub_name)
168 call parse_block_integer(blk, line, 1, this%instr_tool)
169 select case (this%instr_tool)
170 case (option__instrumentfunctions__verrou)
171 write(stderr,'(a)') "Instrumenting " // trim(this%instr_sub_name) // " for Verrou"
172#if !defined(HAVE_VERROU)
173 write(stderr,'(a)') "requires VERROU but that library was not linked."
174 call mpi_world%abort()
175 call loct_exit_failure()
176#endif
177 case (option__instrumentfunctions__fenv)
178 write(stderr,'(a)') "Instrumenting " // trim(this%instr_sub_name) // " with floating-point exceptions"
179 case default
180 assert(.false.) ! Should not happen
181 end select
182 end do
183 call parse_block_end(blk)
184 end if
185
186 call mpi_debug_init(mpi_world%rank, this%info)
187
188 if (this%info) then
189 !%Variable MPIDebugHook
190 !%Type logical
191 !%Default no
192 !%Section Execution::Debug
193 !%Description
194 !% When debugging the code in parallel it is usually difficult to find the origin
195 !% of race conditions that appear in MPI communications. This variable introduces
196 !% a facility to control separate MPI processes. If set to yes, all nodes will
197 !% start up, but will get trapped in an endless loop. In every cycle of the loop
198 !% each node is sleeping for one second and is then checking if a file with the
199 !% name <tt>node_hook.xxx</tt> (where <tt>xxx</tt> denotes the node number) exists. A given node can
200 !% only be released from the loop if the corresponding file is created. This allows
201 !% to selectively run, <i>e.g.</i>, a compute node first followed by the master node. Or, by
202 !% reversing the file creation of the node hooks, to run the master first followed
203 !% by a compute node.
204 !%End
205 call parse_variable(global_namespace, 'MPIDebugHook', .false., mpi_debug_hook)
206 if (mpi_debug_hook) then
207 call loct_gettimeofday(sec, usec)
208 call epoch_time_diff(sec,usec)
209 write(stdout,'(a,i6,a,i6.6,20x,a)') '* I ',sec,'.',usec,' | MPI debug hook'
210
211 write(stdout,'(a,i3,a)') 'node:', mpi_world%rank, ' In debug hook'
212 write(node_hook,'(i3.3)') mpi_world%rank
213 file_exists = .false.
214
215 do while (.not. file_exists)
216 inquire(file='node_hook.'//node_hook, exist=file_exists)
217 call loct_nanosleep(1,0)
218 write(stdout,'(a,i3,a)') 'node:', mpi_world%rank, &
219 ' - still sleeping. To release me touch: node_hook.'//trim(node_hook)
220 end do
221
222 write(stdout,'(a,i3,a)') 'node:', mpi_world%rank, ' Leaving debug hook'
223 ! remove possible debug hooks
224 call loct_rm('node_hook.'//trim(node_hook))
225
226 call loct_gettimeofday(sec, usec)
227 call epoch_time_diff(sec,usec)
228 write(stdout,'(a,i6,a,i6.6,20x,a)') '* O ', sec, '.', usec,' | MPI debug hook'
229 end if
230 end if
231
232 end subroutine debug_init
233
234 !--------------------------------------------------
235
236 subroutine debug_enable(this)
237 type(debug_t), intent(inout) :: this
238
239 this%info = .true.
240 this%trace = .true.
241 this%trace_term = .true.
242 this%trace_file = .true.
243 this%interaction_graph = .true.
244 this%interaction_graph_full = .true.
245 this%propagation_graph = .true.
246
247 end subroutine debug_enable
248
249 !--------------------------------------------------
250
251 subroutine debug_disable(this)
252 type(debug_t), intent(inout) :: this
253
254 call from_bits(this)
255
256 end subroutine debug_disable
257
258 !--------------------------------------------------
259
260 subroutine debug_delete_trace()
261
262 integer :: iunit
263 character(len=6) :: filenum
264
265 iunit = mpi_world%rank + unit_offset
266 write(filenum, '(i6.6)') iunit - unit_offset
267 call loct_mkdir('debug')
268 call loct_rm('debug/debug_trace.node.'//filenum)
269
270 end subroutine debug_delete_trace
271
272 ! ---------------------------------------------------------
273
274 subroutine debug_open_trace(iunit)
275 integer, intent(out) :: iunit
276
277 character(len=6) :: filenum
278
279 iunit = mpi_world%rank + unit_offset
280 write(filenum, '(i6.6)') iunit - unit_offset
281 call loct_mkdir('debug')
282 open(iunit, file = 'debug/debug_trace.node.'//filenum, &
283 action='write', status='unknown', position='append')
284
285 end subroutine debug_open_trace
286
287 ! ---------------------------------------------------------
288
289 subroutine from_bits(this)
290 type(debug_t), intent(inout) :: this
291
292 this%info = (bitand(this%bits, option__debug__info) /= 0)
293 this%trace_term = (bitand(this%bits, option__debug__trace_term) /= 0)
294 this%trace_file = (bitand(this%bits, option__debug__trace_file) /= 0)
295 this%trace = (bitand(this%bits, option__debug__trace) /= 0) .or. this%trace_term .or. this%trace_file
296 this%extra_checks = (bitand(this%bits, option__debug__extra_checks) /= 0) .or. this%trace_term .or. this%trace_file
297 this%interaction_graph = (bitand(this%bits, option__debug__interaction_graph) /= 0)
298 this%interaction_graph_full = (bitand(this%bits, option__debug__interaction_graph_full) /= 0)
299 this%propagation_graph = (bitand(this%bits, option__debug__propagation_graph) /= 0)
300 this%instrument = (bitand(this%bits, option__debug__instrument) /= 0)
301
302 end subroutine from_bits
303
304
305 ! ---------------------------------------------------------
306 subroutine epoch_time_diff(sec, usec)
307 integer, intent(inout) :: sec
308 integer, intent(inout) :: usec
309
310 ! this is called by push/pop so there cannot be a push/pop in this routine
311
312 call time_diff(s_epoch_sec, s_epoch_usec, sec, usec)
313 end subroutine epoch_time_diff
314
315
316 ! ---------------------------------------------------------
319 subroutine time_diff(sec1, usec1, sec2, usec2)
320 integer, intent(in) :: sec1
321 integer, intent(in) :: usec1
322 integer, intent(inout) :: sec2
323 integer, intent(inout) :: usec2
324
325 ! this is called by push/pop so there cannot be a push/pop in this routine
326
327 ! Correct overflow.
328 if (usec2 - usec1 < 0) then
329 usec2 = 1000000 + usec2
330 if (sec2 >= sec1) then
331 sec2 = sec2 - 1
332 end if
333 end if
334
335 ! Replace values.
336 if (sec2 >= sec1) then
337 sec2 = sec2 - sec1
338 end if
339 usec2 = usec2 - usec1
340
341 end subroutine time_diff
342
343
344#ifndef NDEBUG
345 ! ---------------------------------------------------------
347 subroutine debug_push_sub(sub_name)
348 character(len=*), intent(in) :: sub_name
349
350 integer, parameter :: max_recursion_level = 50
351 integer iunit, sec, usec
352
353 if (debug%instrument) then
354 if (debug_clean_path(sub_name) == trim(debug%instr_sub_name)) then
355 select case (debug%instr_tool)
356 case (option__instrumentfunctions__verrou)
358 case (option__instrumentfunctions__fenv)
360 case default
361 assert(.false.) ! cannot happen
362 end select
363 end if
364 end if
365
366 if (.not. debug%trace) return
368 call loct_gettimeofday(sec, usec)
369 call epoch_time_diff(sec, usec)
370
372 if (no_sub_stack >= max_recursion_level) then
373 sub_stack(max_recursion_level) = 'debug_push_sub'
374 write(stderr, '(a,i3,a)') 'Too many recursion levels in debug trace (max=', max_recursion_level, ')'
375 call mpi_world%abort()
376 stop
377 end if
378
379 sub_stack(no_sub_stack) = trim(debug_clean_path(sub_name))
381
382 if (debug%trace_file) then
383 call debug_open_trace(iunit)
384 call push_sub_write(iunit)
385 ! close file to ensure flushing
386 close(iunit)
387 end if
388
389 if (debug%trace_term .and. mpi_grp_is_root(mpi_world)) then
390 ! write to stderr if we are node 0
391 call push_sub_write(stderr)
392 end if
393
394 contains
395
396 subroutine push_sub_write(iunit_out)
397 integer, intent(in) :: iunit_out
398
399 integer :: ii
400 character(len=1000) :: tmpstr
401
402 write(tmpstr,'(a,i6,a,i6.6,f20.6,i8,a)') "* I ", &
403 sec, '.', usec, &
404 loct_clock(), &
405 loct_get_memory_usage() / 1024, " | "
406 do ii = no_sub_stack - 1, 1, -1
407 write(tmpstr, '(2a)') trim(tmpstr), "..|"
408 end do
409 write(tmpstr, '(2a)') trim(tmpstr), trim(debug_clean_path(sub_name))
410 write(iunit_out, '(a)') trim(tmpstr)
411
412 end subroutine push_sub_write
413
414 end subroutine debug_push_sub
415
416 ! ---------------------------------------------------------
418 subroutine debug_pop_sub(sub_name)
419 character(len=*), intent(in) :: sub_name
420
421 character(len=80) :: sub_name_short
422 integer iunit, sec, usec
423
424 if (debug%instrument) then
425 if (debug_clean_path(sub_name) == trim(debug%instr_sub_name)) then
426 select case (debug%instr_tool)
427 case (option__instrumentfunctions__verrou)
429 case (option__instrumentfunctions__fenv)
431 case default
432 assert(.false.) ! cannot happen
433 end select
434 end if
435 end if
436
437 if (.not. debug%trace) return
438
439 call loct_gettimeofday(sec, usec)
440 call epoch_time_diff(sec, usec)
441
442 if (no_sub_stack <= 0) then
443 no_sub_stack = 1
444 sub_stack(1) = 'pop_sub'
445 write(stderr, '(a)') 'Too few recursion levels in debug trace'
446 call mpi_world%abort()
447 stop
448 end if
449
450 ! the name might be truncated in sub_stack, so we copy to a string
451 ! of the same size
452 sub_name_short = trim(debug_clean_path(sub_name))
453
454 if (sub_name_short /= sub_stack(no_sub_stack)) then
455 write(stderr, '(a)') 'Wrong sub name on pop_sub :'
456 write(stderr, '(2a)') ' got : ', sub_name_short
457 write(stderr, '(2a)') ' expected : ', sub_stack(no_sub_stack)
458 call mpi_world%abort()
459 stop
460 end if
461
462 if (debug%trace_file) then
463 call debug_open_trace(iunit)
464 call pop_sub_write(iunit)
465 ! close file to ensure flushing
466 close(iunit)
467 end if
468
469 if (debug%trace_term .and. mpi_grp_is_root(mpi_world)) then
470 ! write to stderr if we are node 0
471 call pop_sub_write(stderr)
472 end if
473
475
476 contains
477
478 subroutine pop_sub_write(iunit_out)
479 integer, intent(in) :: iunit_out
480
481 integer :: ii
482 character(len=1000) :: tmpstr
483
484 write(tmpstr,'(a,i6,a,i6.6,f20.6,i8, a)') "* O ", &
485 sec, '.', usec, &
487 loct_get_memory_usage() / 1024, " | "
488 do ii = no_sub_stack - 1, 1, -1
489 write(tmpstr,'(2a)') trim(tmpstr), "..|"
490 end do
491 write(tmpstr,'(2a)') trim(tmpstr), trim(sub_stack(no_sub_stack))
492
493 write(iunit_out, '(a)') trim(tmpstr)
494
495 end subroutine pop_sub_write
496
497 end subroutine debug_pop_sub
498#endif
499
500 ! -----------------------------------------------------------
502 character(len=MAX_PATH_LEN) function debug_clean_path(filename) result(clean_path)
503 character(len=*), intent(in) :: filename
504
505 integer :: pos
506
507 pos = index(filename, 'src/', back = .true.)
508 if (pos == 0) then
509 ! 'src/' does not occur
510 clean_path = filename
511 else
512 ! remove 'src/'
513 clean_path = filename(pos+4:)
514 end if
515
516 end function debug_clean_path
517
518end module debug_oct_m
519
520!! Local Variables:
521!! mode: f90
522!! coding: utf-8
523!! End:
subroutine pop_sub_write(iunit_out)
Definition: debug.F90:572
subroutine push_sub_write(iunit_out)
Definition: debug.F90:490
File-handling.
Definition: loct.F90:216
character(len=max_path_len) function, public debug_clean_path(filename)
Prune a filename path to only include subdirectories of the "src" directory.
Definition: debug.F90:596
subroutine, public debug_enable(this)
Definition: debug.F90:330
type(debug_t), save, public debug
Definition: debug.F90:156
subroutine, public debug_pop_sub(sub_name)
Pop a routine from the debug trace.
Definition: debug.F90:512
subroutine, public debug_open_trace(iunit)
Definition: debug.F90:368
subroutine, public epoch_time_diff(sec, usec)
Definition: debug.F90:400
subroutine from_bits(this)
Definition: debug.F90:383
subroutine, public debug_init(this, namespace)
Definition: debug.F90:178
subroutine, public debug_disable(this)
Definition: debug.F90:345
subroutine time_diff(sec1, usec1, sec2, usec2)
Computes t2 <- t2-t1. sec1,2 and usec1,2 are seconds,microseconds of t1,2.
Definition: debug.F90:413
subroutine, public debug_push_sub(sub_name)
Push a routine to the debug trace.
Definition: debug.F90:441
subroutine, public debug_delete_trace()
Definition: debug.F90:354
integer, public s_epoch_sec
global epoch time (time at startup)
Definition: global.F90:235
integer, public no_sub_stack
Definition: global.F90:240
real(real64), dimension(50), public time_stack
Definition: global.F90:239
character(len=80), dimension(50), public sub_stack
The stack.
Definition: global.F90:238
integer, public s_epoch_usec
Definition: global.F90:235
subroutine, public mpi_debug_init(rank, info)
Definition: mpi_debug.F90:182
logical function mpi_grp_is_root(grp)
Is the current MPI process of grpcomm, root.
Definition: mpi.F90:430
type(mpi_grp_t), public mpi_world
Definition: mpi.F90:266
type(namespace_t), public global_namespace
Definition: namespace.F90:132
integer function, public parse_block(namespace, name, blk, check_varinfo_)
Definition: parser.F90:618
int true(void)