|
| 1 | +#include <unistd.h> |
| 2 | +#include <stdio.h> // puts, printf, scanf |
| 3 | +#include <time.h> // ctime, time |
| 4 | +#include <string.h> // memcpy, memcmp |
| 5 | + |
| 6 | +#include <semaphore.h> // sem_t |
| 7 | +typedef sem_t *SEM_HANDLE; |
| 8 | + |
| 9 | +#define MAX_SEMAPHORES_SHOW 32 |
| 10 | + |
| 11 | +#include "../semaphore_macosx.h" |
| 12 | +#include "shared_mem.h" |
| 13 | + |
| 14 | +// Static datas for each process. |
| 15 | +CountersWorkaround shm_semlock_counters = { |
| 16 | + .state_this = THIS_NOT_OPEN, |
| 17 | + .name_shm = "/shm_gh125828", |
| 18 | + .handle_shm = (MEMORY_HANDLE)0, |
| 19 | + .create_shm = 0, |
| 20 | + .name_shm_lock = "/mp_gh125828", |
| 21 | + .handle_shm_lock = (SEM_HANDLE)0, |
| 22 | + .header = (HeaderObject *)NULL, |
| 23 | + .counters = (CounterObject *)NULL, |
| 24 | +}; |
| 25 | + |
| 26 | +HeaderObject *header = NULL; |
| 27 | +CounterObject *counter = NULL; |
| 28 | + |
| 29 | +static char *show_counter(char *p, CounterObject *counter) { |
| 30 | + sprintf(p, "p:%p, n:%s, v:%d, u:%d, t:%s", counter, |
| 31 | + counter->sem_name, |
| 32 | + counter->internal_value, |
| 33 | + counter->unlink_done, |
| 34 | + ctime(&counter->ctimestamp)); |
| 35 | + return p; |
| 36 | +} |
| 37 | + |
| 38 | +static void dump_shm_semlock_counters(void) { |
| 39 | +puts(__func__); |
| 40 | + |
| 41 | + char buf[256]; |
| 42 | + int i = 0, j = 0; |
| 43 | + |
| 44 | + if (shm_semlock_counters.state_this == THIS_AVAILABLE) { |
| 45 | + CounterObject *counter = shm_semlock_counters.counters; |
| 46 | + HeaderObject *header = shm_semlock_counters.header; |
| 47 | + dump_shm_semlock_header_counters(); |
| 48 | + dump_shm_semlock_header(); |
| 49 | + int show_max = header->n_semlocks > MAX_SEMAPHORES_SHOW ? MAX_SEMAPHORES_SHOW : header->n_semlocks; |
| 50 | + for(; i < header->n_slots && j < show_max; i++, counter++ ) { |
| 51 | + if (counter->sem_name[0] != 0) { |
| 52 | + printf("%s", show_counter(buf, counter)); |
| 53 | + ++j; |
| 54 | + } |
| 55 | + } |
| 56 | + if (show_max < header->n_semlocks) { |
| 57 | + printf("......\n--------- More %d Semphores ---------\n", header->n_semlocks-show_max); |
| 58 | + } |
| 59 | + } |
| 60 | +} |
| 61 | + |
| 62 | +int main(int argc, char *argv[]) { |
| 63 | + int repeat = 0; |
| 64 | + long udelay = 5000; |
| 65 | + HeaderObject save = {0}; |
| 66 | + int unlink = 0; |
| 67 | + int force_open = 1; |
| 68 | + int release_lock = 1; |
| 69 | + |
| 70 | + puts("--------"); |
| 71 | + printf("PID:%d, PPID:%d\n", getpid(), getppid()); |
| 72 | + connect_shm_semlock_counters(unlink, force_open, release_lock); |
| 73 | + puts("+++++++++"); |
| 74 | + if (argc > 1) { |
| 75 | + sscanf(argv[1], "%d", &repeat); |
| 76 | + if (argc >= 2) { |
| 77 | + puts(argv[2]); |
| 78 | + sscanf(argv[2], "%lu", &udelay); |
| 79 | + } |
| 80 | + } else { |
| 81 | + puts("dump_shared_mem <repeat> <delay> where:\n repeat (-1 " |
| 82 | + "is infinite) and a delay (us) between two dumps \n"); |
| 83 | + return 1; |
| 84 | + } |
| 85 | + |
| 86 | + printf("Repeat:%d, udelay:%lu\n", repeat, udelay); |
| 87 | + |
| 88 | + if (shm_semlock_counters.state_this == THIS_AVAILABLE) { |
| 89 | + memset(&save, '\0', sizeof(save)); |
| 90 | + do { |
| 91 | + if (memcmp(&save, shm_semlock_counters.header, sizeof(HeaderObject)) ) { |
| 92 | + time_t timestamp = time(NULL); |
| 93 | + puts(ctime(×tamp)); |
| 94 | + dump_shm_semlock_counters(); |
| 95 | + memcpy(&save, shm_semlock_counters.header, sizeof(HeaderObject)); |
| 96 | + puts("=========="); |
| 97 | + } |
| 98 | + usleep(udelay); |
| 99 | + } while(repeat--); |
| 100 | + } |
| 101 | + return 1; |
| 102 | +} |
0 commit comments