static void alarm_handler(int signal) { finish_time = rdclock(); finish_number = current_number; gracefully_exit(); // Should have already exited.. printf("Graceful exit did not exit!?\n"); exit(-1); }
static int bench_cpu(int argc, char **argv) { ull noop_index = 0; parse_opts(argc, argv); if(end_time == ~0 && end_number == ~0) { bzero(error_msg, strlen(error_msg)); sprintf(error_msg, "No options specified\nTerminating program due to infinite loop!\n"); panic(); } ull malloc_size; if(end_number != ~0) malloc_size = sizeof(ull) * (end_number / 2); else malloc_size = sizeof(ull) * 10 * 1024; //Allocate 10K primes = malloc(malloc_size); bzero(primes, malloc_size); append(primes, 2); signal(SIGALRM, alarm_handler); setitimer(ITIMER_REAL, &timeout_timer, 0); start_time = rdclock(); while(1) { is_prime(current_number); if(current_number == end_number) break; current_number++; } alarm_handler(SIGALRM); }
int perf_event__synthesize_mmap_events(struct perf_tool *tool, union perf_event *event, pid_t pid, pid_t tgid, perf_event__handler_t process, struct machine *machine, bool mmap_data, unsigned int proc_map_timeout) { char filename[PATH_MAX]; FILE *fp; unsigned long long t; bool truncation = false; unsigned long long timeout = proc_map_timeout * 1000000ULL; int rc = 0; if (machine__is_default_guest(machine)) return 0; snprintf(filename, sizeof(filename), "%s/proc/%d/maps", machine->root_dir, pid); fp = fopen(filename, "r"); if (fp == NULL) { /* * We raced with a task exiting - just return: */ pr_debug("couldn't open %s\n", filename); return -1; } event->header.type = PERF_RECORD_MMAP2; t = rdclock(); while (1) { char bf[BUFSIZ]; char prot[5]; char execname[PATH_MAX]; char anonstr[] = "//anon"; unsigned int ino; size_t size; ssize_t n; if (fgets(bf, sizeof(bf), fp) == NULL) break; if ((rdclock() - t) > timeout) { pr_warning("Reading %s time out. " "You may want to increase " "the time limit by --proc-map-timeout\n", filename); truncation = true; goto out; } /* ensure null termination since stack will be reused. */ strcpy(execname, ""); /* 00400000-0040c000 r-xp 00000000 fd:01 41038 /bin/cat */ n = sscanf(bf, "%"PRIx64"-%"PRIx64" %s %"PRIx64" %x:%x %u %[^\n]\n", &event->mmap2.start, &event->mmap2.len, prot, &event->mmap2.pgoff, &event->mmap2.maj, &event->mmap2.min, &ino, execname); /* * Anon maps don't have the execname. */ if (n < 7) continue; event->mmap2.ino = (u64)ino; /* * Just like the kernel, see __perf_event_mmap in kernel/perf_event.c */ if (machine__is_host(machine)) event->header.misc = PERF_RECORD_MISC_USER; else event->header.misc = PERF_RECORD_MISC_GUEST_USER; /* map protection and flags bits */ event->mmap2.prot = 0; event->mmap2.flags = 0; if (prot[0] == 'r') event->mmap2.prot |= PROT_READ; if (prot[1] == 'w') event->mmap2.prot |= PROT_WRITE; if (prot[2] == 'x') event->mmap2.prot |= PROT_EXEC; if (prot[3] == 's') event->mmap2.flags |= MAP_SHARED; else event->mmap2.flags |= MAP_PRIVATE; if (prot[2] != 'x') { if (!mmap_data || prot[0] != 'r') continue; event->header.misc |= PERF_RECORD_MISC_MMAP_DATA; } out: if (truncation) event->header.misc |= PERF_RECORD_MISC_PROC_MAP_PARSE_TIMEOUT; if (!strcmp(execname, "")) strcpy(execname, anonstr); size = strlen(execname) + 1; memcpy(event->mmap2.filename, execname, size); size = PERF_ALIGN(size, sizeof(u64)); event->mmap2.len -= event->mmap.start; event->mmap2.header.size = (sizeof(event->mmap2) - (sizeof(event->mmap2.filename) - size)); memset(event->mmap2.filename + size, 0, machine->id_hdr_size); event->mmap2.header.size += machine->id_hdr_size; event->mmap2.pid = tgid; event->mmap2.tid = pid; if (perf_tool__process_synth_event(tool, event, machine, process) != 0) { rc = -1; break; } if (truncation) break; } fclose(fp); return rc; }