28 #include <sys/types.h>
43 #include "debug_lock.h"
44 #include "shm_arena.h"
46 #include "arena_lock.h"
69 arena = get_arena_and_autolock(arena, 1, &err IF_SPEW(, __func__));
78 printf(
"Bad magic number 0x%X != 0x%X\n",
90 if(_shm_arena_sync_mappings(IF_SPEW(__func__,) arena))
92 SPEW_SYS_ERR(_WARN, ret,
"%s() failed", __func__);
107 map_header = get_mapping_header(arena, i);
109 while((uintptr_t)seg <
113 footer = get_seg_footer(seg);
115 if(!(footer->
flags & IS_FREE))
116 printf(
"%zu %s\n", seg->
user_length, get_seg_name(seg));
122 printf(
"***ERROR***** seg->length = 0\n");
125 else if((uintptr_t) (seg->
length*CHUNK) >
127 ((uintptr_t)(seg))-((uintptr_t)(arena->
mapping[i].
start))))
129 printf(
"***ERROR***** seg->length = "OFFSET_FORMAT
130 " CHUNKS IS TOO LARGE for this mapping\n", seg->
length);
140 if(i<arena->num_mappings)
142 map_header = get_mapping_header(arena, i);
148 err = arena_autounlock(arena IF_SPEW(, __func__));
153 SPEW(_WARN,
"We're screwed");
163 err = arena_autounlock(arena IF_SPEW(, __func__));
168 SPEW(_WARN,
"We're screwed");
199 arena = get_arena_and_autolock(arena, 1, &err IF_SPEW(, __func__));
206 pagesize = getpagesize();
209 printf(
"Bad magic number 0x%X != 0x%X\n",
221 if(_shm_arena_sync_mappings(IF_SPEW(__func__,) arena))
223 SPEW_SYS_ERR(_WARN, ret,
"%s() failed", __func__);
231 printf(
"-------------------------------------------------------------\n");
233 printf(
"pagesize = %zu bytes CHUNK = %zu bytes\n\n",
236 printf(
" " MAPNUM_FORMAT
" mapping(s)\n", arena->
num_mappings);
238 printf(
"--------------------------------------------------------------------\n"
239 "-------------- MAPPING 0 at %p -------------------------\n"
240 "--------------------------------------------------------------------\n"
241 " struct arena_header size= %zu bytes = %zu CHUNKS\n",
245 map_header = get_mapping_header(arena, 0);
246 printf(
" ----------------------------------------------------------\n"
247 " struct mapping_header size= %zu CHUNK(s) = %zu bytes \n"
248 " -------------------------------------------------------\n"
249 " map_length = "OFFSET_FORMAT
" CHUNKS = %zu bytes = %zu pages\n"
250 " length_used = "OFFSET_FORMAT
" CHUNKS = %zu bytes\n"
251 " -------------------------------------------------------\n",
259 #define SHOW_FLAG(x,f) (((x)&(f))?("|" #f):"")
261 printf(
" flags = 0");
264 #define SHOW_FLAG(x,f) (((x)&(f))?("|" #f):"")
273 printf(
" free_mapnum = "MAPNUM_FORMAT
"\r",
278 printf(
" free_offset = "OFFSET_FORMAT
" CHUNKS\r",
280 printf(
" alloc_offset = "OFFSET_FORMAT
" CHUNKS\n",
293 map_header = get_mapping_header(arena, i+1);
295 printf(
" ------------------------------------------------------------\n"
296 " details for next mapping (mapping %d)\n"
297 " struct mapping_header size= %zu bytes = %zu CHUNK(S)\n"
298 " ------------------------------------------------------------\n"
299 " map_length = "OFFSET_FORMAT
" CHUNKS = %zu bytes = %zu pages\n"
300 " length_used = "OFFSET_FORMAT
" CHUNKS = %zu bytes\n"
301 " ------------------------------------------------------------\n",
309 map_header = get_mapping_header(arena, i);
311 while((uintptr_t)seg <
315 footer = get_seg_footer(seg);
317 printf(
" -----------------------------------------------------------\n");
319 if(!(footer->
flags & IS_FREE))
320 printf(
" \"%s\"", get_seg_name(seg));
324 printf(
" segment at %p %s read-write lock\n"
325 " %"PRIuPTR
" CHUNKS from mapping %d start\n"
326 " right_mapnum = "MAPNUM_FORMAT
"\r"
327 " left_mapnum = "MAPNUM_FORMAT
"\n"
328 " right_offset = "OFFSET_FORMAT
" CHUNKS\r"
329 " left_offset = "OFFSET_FORMAT
" CHUNKS\n"
331 " length = "OFFSET_FORMAT
" CHUNKS = %zu bytes\n"
332 " user_length = %zu bytes %s %zu CHUNK(S)\n"
333 " height = "HEIGHT_FORMAT
"\n",
334 seg, (footer->
flags & WITH_RWLOCK)?
"with":
"without",
335 (((uintptr_t) seg) - ((uintptr_t) arena->
mapping[i].
start))/CHUNK,
337 seg->right_mapnum, seg->left_mapnum,
338 seg->right_offset, seg->left_offset,
339 seg->length, seg->length*CHUNK,
341 (seg->user_length != (seg->user_length/CHUNK)*CHUNK)?
"uses":
"=",
342 CHUNKS(seg->user_length)/CHUNK,
345 if(footer->
length != seg->length)
346 printf(
"***ERROR****THE FOOTER length ("OFFSET_FORMAT
" CHUNKS) does not match "
347 "the HEADER length\n", footer->
length);
349 printf(
" -----------------------------------------------------------\n");
353 printf(
"***ERROR***** seg->length = 0\n");
356 else if((uintptr_t) (seg->length*CHUNK) >
358 ((uintptr_t)(seg))-((uintptr_t)(arena->
mapping[i].
start))))
360 printf(
"***ERROR***** seg->length = "OFFSET_FORMAT
361 " CHUNKS IS TOO LARGE for this mapping\n", seg->length);
366 seg = (
struct seg_header *)(((uint8_t *) seg) + seg->length*CHUNK);
371 if(i<arena->num_mappings)
373 printf(
"--------------------------------------------------------------------\n"
374 "-------------- MAPPING %d at %p -------------------------\n"
375 "--------------------------------------------------------------------\n",
377 map_header = get_mapping_header(arena, i);
382 printf(
"--------------------------------------------------------------------\n");
386 err = arena_autounlock(arena IF_SPEW(, __func__));
391 SPEW(_WARN,
"We're screwed");
401 err = arena_autounlock(arena IF_SPEW(, __func__));
406 SPEW(_WARN,
"We're screwed");
417 void print_alloc_node(
shm_arena_t arena, mapnum_t mapnum,
422 seg = get_seg_header(arena, mapnum, offset);
426 name = get_seg_name(seg);
428 printf(
" -> n"MAPNUM_FORMAT
"x"OFFSET_FORMAT
";\n",
430 printf(
" n"MAPNUM_FORMAT
"x"OFFSET_FORMAT
431 " [label=\"%s\\ns=%zu\\nh="HEIGHT_FORMAT
"\"];\n",
436 printf(
" n"MAPNUM_FORMAT
"x"OFFSET_FORMAT,
442 printf(
" n"MAPNUM_FORMAT
"x"OFFSET_FORMAT
443 " -> l"MAPNUM_FORMAT
"x"OFFSET_FORMAT
" [style=dotted,arrowhead=dot];\n",
444 mapnum, offset, mapnum, offset);
445 printf(
" l"MAPNUM_FORMAT
"x"OFFSET_FORMAT
" [label=\"\",color=white];\n",
451 printf(
" n"MAPNUM_FORMAT
"x"OFFSET_FORMAT,
457 printf(
" n"MAPNUM_FORMAT
"x"OFFSET_FORMAT
458 " -> r"MAPNUM_FORMAT
"x"OFFSET_FORMAT
" [style=dotted,arrowhead=dot];\n",
459 mapnum, offset, mapnum, offset);
460 printf(
" r"MAPNUM_FORMAT
"x"OFFSET_FORMAT
" [label=\"\",color=white];\n",
467 void print_free_node(
shm_arena_t arena, mapnum_t mapnum,
471 seg = get_seg_header(arena, mapnum, offset);
476 printf(
" -> n"MAPNUM_FORMAT
"x"OFFSET_FORMAT
";\n",
478 printf(
" n"MAPNUM_FORMAT
"x"OFFSET_FORMAT
479 " [label=\"m="MAPNUM_FORMAT
"\\ns="OFFSET_FORMAT
"\\nof="
480 OFFSET_FORMAT
"\\nh="HEIGHT_FORMAT
"\"];\n",
481 mapnum, offset, mapnum, seg->
length, offset, seg->
height);
485 printf(
" n"MAPNUM_FORMAT
"x"OFFSET_FORMAT,
491 printf(
" n"MAPNUM_FORMAT
"x"OFFSET_FORMAT
492 " -> l"MAPNUM_FORMAT
"x"OFFSET_FORMAT
" [style=dotted,arrowhead=dot];\n",
493 mapnum, offset, mapnum, offset);
494 printf(
" l"MAPNUM_FORMAT
"x"OFFSET_FORMAT
" [label=\"\",color=white];\n",
500 printf(
" n"MAPNUM_FORMAT
"x"OFFSET_FORMAT,
506 printf(
" n"MAPNUM_FORMAT
"x"OFFSET_FORMAT
507 " -> r"MAPNUM_FORMAT
"x"OFFSET_FORMAT
" [style=dotted,arrowhead=dot];\n",
508 mapnum, offset, mapnum, offset);
509 printf(
" r"MAPNUM_FORMAT
"x"OFFSET_FORMAT
" [label=\"\",color=white];\n",
540 arena = get_arena_and_autolock(arena, 1, &err IF_SPEW(, __func__));
550 SPEW_SYS_ERR(_WARN, ret,
"Bad magic number 0x%X != 0x%X\n",
561 if(_shm_arena_sync_mappings(IF_SPEW(__func__,) arena))
563 SPEW_SYS_ERR(_WARN, ret,
"%s() failed", __func__);
570 printf(
"digraph G {\n\n");
571 printf(
"bgcolor =\"transparent\";\n");
572 printf(
" node [style=filled,color=\"0.7 0.3 10\"]\n");
574 printf(
"\"allocated\\nsegments\"");
586 printf(
"\n\n node [color=\"0.4 0.8 10\"]\n");
588 printf(
"\n \"free\\nsegments\"");
602 err = arena_autounlock(arena IF_SPEW(, __func__));
607 SPEW(_WARN,
"We're screwed");
617 err = arena_autounlock(arena IF_SPEW(, __func__));
622 SPEW(_WARN,
"We're screwed");
644 arena = get_arena_and_autolock(arena, 1, &err IF_SPEW(, __func__));
654 SPEW_SYS_ERR(_WARN, ret,
"Bad magic number 0x%X != 0x%X\n",
665 if(_shm_arena_sync_mappings(IF_SPEW(__func__,) arena))
667 SPEW_SYS_ERR(_WARN, ret,
"%s() failed", __func__);
679 map_header = get_mapping_header(arena, i);
685 while((uintptr_t)seg <
692 f = get_seg_footer(seg);
694 if(f->
flags & IS_FREE)
696 if(seg != _shm_check_free_segment(arena, i,
697 get_offset(arena, seg, i)))
700 printf(
"did not find free segment in AVL tree\n");
701 printf(
"mapping=%d offset="OFFSET_FORMAT
" CHUNKS\n",
702 i, get_offset(arena, seg, i));
708 if(seg != find_segment(arena, get_seg_name(seg), &mapnum))
711 printf(
"did not find allocated segment \"%s\" in AVL tree\n",
713 printf(
"mapping=%d offset="OFFSET_FORMAT
" CHUNKS\n",
714 i, get_offset(arena, seg, i));
720 printf(
"***ERROR***** seg->length = 0\n");
723 else if((uintptr_t) (seg->
length*CHUNK) >
725 ((uintptr_t)(seg))-((uintptr_t)(arena->
mapping[i].
start))))
727 printf(
"***ERROR***** seg->length = "OFFSET_FORMAT
728 " CHUNKS IS TOO LARGE for this mapping\n", seg->
length);
732 diff = difference(arena, seg);
734 if(diff > 1 || diff < -1)
737 printf(
"%s segment \"%s\" is not balanced: diff=%d "
738 "%s child is higher\n",
739 (f->
flags & IS_FREE)?
"free":
"allocated",
740 (f->
flags & IS_FREE)?
"":get_seg_name(seg),
741 diff, (diff>0)?
"left":
"right");
742 printf(
"mapping=%d offset="OFFSET_FORMAT
" CHUNKS\n",
743 i, get_offset(arena, seg, i));
753 err = arena_autounlock(arena IF_SPEW(, __func__));
758 SPEW(_WARN,
"We're screwed");
struct arena_header * header
struct shm_mapping * mapping
pthread_mutex_t mapping_mutex
int shm_arena_print_list(shm_arena_t arena)
#define SHM_WITH_SHM_OPEN
shm_arena_create() flag to use shm_open()
int shm_arena_print_dot(shm_arena_t arena)
print debug information about a shared memory arena object in this case a dot file that can be made i...
int shm_arena_print(shm_arena_t arena)
print the cotents of a shared memory arena file to stdout