/* Main execution loop in live mode. Builds the list of processes, * collects statistics, and prints using curses. Repeats after some * delay, also catching key presses. */ static int live_mode(struct process_list* proc_list, screen_t* screen) { WINDOW* help_win = NULL; WINDOW* error_win = NULL; fd_set fds; struct process** p; int num_iter = 0; int with_colors = 0; int pos; /* start curses */ initscr(); cbreak(); noecho(); keypad(stdscr, TRUE); /* Prepare help window */ help_win = prepare_help_win(screen); if (has_colors()) { /* initialize curses colors */ with_colors = 1; start_color(); init_pair(1, COLOR_BLACK, COLOR_WHITE); init_pair(2, COLOR_WHITE, COLOR_BLACK); init_pair(3, COLOR_GREEN, COLOR_BLACK); init_pair(4, COLOR_YELLOW, COLOR_BLACK); init_pair(5, COLOR_RED, COLOR_BLACK); attron(COLOR_PAIR(0)); } tv.tv_sec = 0; tv.tv_usec = 200000; /* 200 ms for first iteration */ header = gen_header(screen, &options, COLS - 1, active_col); pos = screen_pos(screen); for(num_iter=0; !options.max_iter || num_iter<options.max_iter; num_iter++) { int i, zz, printed, num_fd, num_dead; /* print various info */ erase(); mvprintw(0, 0, "tiptop -"); if ((num_errors() > 0) && (COLS >= 37)) mvprintw(LINES-1, 30, "[errors]"); if ((options.config_file == 1) && (COLS >= 60)) mvprintw(0, COLS-60, "[conf]"); if ((options.euid == 0) && (COLS >= 54)) mvprintw(0, COLS-54, "[root]"); if ((options.watch_uid != -1) && (COLS >= 48)) mvprintw(0, COLS-48, "[uid]"); if ((options.only_pid || options.only_name) && (COLS >= 43)) mvprintw(0, COLS-43, "[pid]"); if (options.show_kernel && (COLS >= 38)) mvprintw(0, COLS-38, "[kernel]"); if (options.sticky && (COLS >= 30)) mvprintw(0, COLS-30, "[sticky]"); if (options.show_threads && (COLS >= 22)) mvprintw(0, COLS-22, "[threads]"); if (options.idle && (COLS >= 13)) mvprintw(0, COLS-13, "[idle]"); if (options.debug && (COLS >= 7)) mvprintw(0, COLS-7, "[debug]"); if (options.show_epoch && (COLS >= 18)) mvprintw(LINES-1, COLS-18, "Epoch: %u", time(NULL)); if (options.show_timestamp) mvprintw(LINES-1, 0, "Iteration: %u", num_iter); /* print main header */ if (with_colors) attron(COLOR_PAIR(1)); mvprintw(3, 0, "%s", header); for(zz=strlen(header); zz < COLS-1; zz++) printw(" "); printw("\n"); if (with_colors) attroff(COLOR_PAIR(1)); /* update the list of processes/threads and accumulate info if needed */ num_dead = update_proc_list(proc_list, screen, &options); if (!options.show_threads) accumulate_stats(proc_list); p = proc_list->proc_ptrs; /* prepare for select */ FD_ZERO(&fds); FD_SET(STDIN_FILENO, &fds); /* generate the text version of all rows */ build_rows(proc_list, screen, COLS - 1); /* sort by %CPU */ qsort(p, proc_list->num_tids, sizeof(struct process*), sorting_fun); printed = 0; /* Iterate over all threads */ for(i=0; i < proc_list->num_tids; i++) { if (p[i]->skip) continue; /* highlight watched process, if any */ if (with_colors) { if (p[i]->dead) { attron(COLOR_PAIR(5)); } else if ((p[i]->tid == options.watch_pid) || (options.watch_name && options.show_cmdline && strstr(p[i]->cmdline, options.watch_name)) || (options.watch_name && !options.show_cmdline && strstr(p[i]->name, options.watch_name))) attron(COLOR_PAIR(3)); } if (options.show_threads || (p[i]->pid == p[i]->tid)) { printw("%s\n", p[i]->txt); printed++; } if (with_colors) attroff(COLOR_PAIR(3)); if (printed >= LINES - 5) /* stop printing at bottom of window */ break; } mvprintw(1, 0, "Tasks: %3d total, %3d displayed", proc_list->num_tids, printed); if (options.sticky) printw(", %3d dead", num_dead); /* print the screen name, make sure it fits, or truncate */ if (with_colors) attron(COLOR_PAIR(4)); if (35 + 20 + 11 + strlen(screen->name) < COLS) { mvprintw(1, COLS - 11 - strlen(screen->name), "screen %2d: %s\n", pos, screen->name); } else if (COLS >= 35 + 20 + 11) { char screen_str[50] = { 0 }; snprintf(screen_str, sizeof(screen_str) - 1, "%s\n", screen->name); screen_str[COLS - 35 - 20 - 11] = '\0'; /* truncate */ mvprintw(1, 35+20, "screen %2d: %s", pos, screen_str); } if (with_colors) attroff(COLOR_PAIR(4)); /* print message if any */ if (message) { if (with_colors) attron(COLOR_PAIR(1)); mvprintw(2, 0, "%s", message); if (with_colors) attroff(COLOR_PAIR(1)); message = NULL; /* reset message */ } refresh(); /* display everything */ if (options.error) { if (options.error == 1) { options.error = 2; show_error_win(error_win, printed); } else show_error_win(error_win, -1); } if (options.help) show_help_win(help_win, screen); if ((num_dead) && (!options.sticky)) compact_proc_list(proc_list); /* wait some delay, or until a key is pressed */ num_fd = select(1 + STDIN_FILENO, &fds, NULL, NULL, &tv); if (num_fd > 0) { int c = handle_key(); if (c == 'q') break; if (c == '>') { if (active_col < screen->num_columns ) active_col++; free(header); header = gen_header(screen, &options, COLS - 1, active_col); } if (c == '<') { if (active_col > -1) active_col--; free(header); header = gen_header(screen, &options, COLS - 1, active_col); } if (c == 'H') { if (options.show_threads) { reset_values(proc_list); message = "Show threads On"; } else message = "Show threads Off"; } if (c == 'U') { free(header); header = gen_header(screen, &options, COLS - 1, active_col); } if ((c == '+') || (c == '-') || (c == KEY_LEFT) || (c == KEY_RIGHT)) return c; if ((c == 'u') || (c == 'K') || (c == 'p')) /* need to rebuild tasks list */ return c; if (c == 'e') { if (options.error > 0) { options.error = 0; delwin(error_win); error_win = NULL; } else options.error = 1; } } tv.tv_sec = options.delay; tv.tv_usec = (options.delay - tv.tv_sec) * 1000000.0; } free(header); delwin(help_win); endwin(); /* stop curses */ return 'q'; }
int main(int argc, char* argv[]) { assert(argc == 8); MPI_Init(NULL, NULL); MPI_Datatype mpi_spike = create_spike_type(); int rank, size; MPI_Comm_rank(MPI_COMM_WORLD, &rank); MPI_Comm_size(MPI_COMM_WORLD, &size); int ngroups = atoi(argv[1]); int simtime = atoi(argv[2]); int ncells = atoi(argv[3]); int fanin = atoi(argv[4]); int nSpikes = atoi(argv[5]); int mindelay = atoi(argv[6]); bool algebra = atoi(argv[7]); struct timeval start, end; int cellsper = ncells / size; //create environment environment::event_generator generator(ngroups); double mean = static_cast<double>(simtime) / static_cast<double>(nSpikes); double lambda = 1.0 / static_cast<double>(mean * size); environment::continousdistribution neuro_dist(size, rank, ncells); environment::generate_events_kai(generator.begin(), simtime, ngroups, rank, size, lambda, &neuro_dist); environment::presyn_maker presyns(fanin); presyns(rank, &neuro_dist); spike::spike_interface s_interface(size); //run simulation MPI_Comm neighborhood = create_dist_graph(presyns, cellsper); queueing::pool pl(algebra, ngroups, mindelay, rank, s_interface); gettimeofday(&start, NULL); while(pl.get_time() <= simtime){ pl.fixed_step(generator, presyns); distributed_spike(s_interface, mpi_spike, neighborhood); pl.filter(presyns); } gettimeofday(&end, NULL); long long diff_ms = (1000 * (end.tv_sec - start.tv_sec)) + ((end.tv_usec - start.tv_usec) / 1000); if(rank == 0){ std::cout<<"run time: "<<diff_ms<<" ms"<<std::endl; } pl.accumulate_stats(); accumulate_stats(s_interface); MPI_Comm_free(&neighborhood); MPI_Type_free(&mpi_spike); MPI_Finalize(); return 0; }
/* Main execution loop in batch mode. Builds the list of processes, * collects statistics, and prints. Repeats after some delay. */ static void batch_mode(struct process_list* proc_list, screen_t* screen) { int num_iter = 0; int num_printed; int pos; FILE* out = options.out; struct process** p; tv.tv_sec = 0; tv.tv_usec = 200000; /* 200 ms for first iteration */ /* Print various information about this run */ fprintf(out, "tiptop - "); fflush(out); { /* uptime */ FILE* f; float val1, val5, val15, up; int days, hours, minutes, n; f = fopen("/proc/loadavg", "r"); n = fscanf(f, "%f %f %f", &val1, &val5, &val15); if (n != 3) val1 = val5 = val15 = 0.0; /* something went wrong, no sure what */ fclose(f); f = fopen("/proc/uptime", "r"); n = fscanf(f, "%f", &up); if (n != 1) up = 0.0; fclose(f); days = up / 86400; hours = (up - days*86400) / 3600; minutes = (up - days*86400 - hours*3600) / 60; fprintf(out, "up %d days, %d:%02d, load average: %.2f, %.2f, %.2f\n", days, hours, minutes, val1, val5, val15); } { /* date */ char outstr[200]; time_t t = time(NULL); struct tm *tmp = localtime(&t); if (tmp) { strftime(outstr, sizeof(outstr), "%a %b %e %H:%M:%S %Z %Y", tmp); fprintf(out, "%s\n", outstr); } } pos = screen_pos(screen); fprintf(out, "delay: %.2f idle: %d threads: %d\n", options.delay, (int)options.idle, (int)options.show_threads); if (options.watch_pid) fprintf(out, "watching pid %d\n", options.watch_pid); else if (options.watch_name) fprintf(out, "watching pid '%s'\n", options.watch_name); if (options.only_pid) fprintf(out, "only pid %d\n", options.only_pid); else if (options.only_name) fprintf(out, "only pid '%s'\n", options.only_name); if (options.watch_uid != -1) { struct passwd* passwd = getpwuid(options.watch_uid); assert(passwd); fprintf(out, "watching uid %d '%s'\n", options.watch_uid, passwd->pw_name); } header = gen_header(screen, &options, TXT_LEN - 1, active_col); fprintf(out, "Screen %d: %s\n", pos, screen->name); fprintf(out, "\n%s\n", header); for(num_iter=0; !options.max_iter || num_iter<options.max_iter; num_iter++) { unsigned int epoch = 0; int i, num_dead; /* update the list of processes/threads and accumulate info if needed */ if (options.show_epoch) epoch = time(NULL); num_dead = update_proc_list(proc_list, screen, &options); if (!options.show_threads) accumulate_stats(proc_list); p = proc_list->proc_ptrs; /* generate the text version of all rows */ build_rows(proc_list, screen, -1); /* sort by %CPU */ qsort(p, proc_list->num_tids, sizeof(struct process*), sorting_fun); num_printed = 0; for(i=0; i < proc_list->num_tids; i++) { if (p[i]->skip) continue; if (options.show_threads || (p[i]->pid == p[i]->tid)) { if (options.show_timestamp) fprintf(out, "%6d ", num_iter); if (options.show_epoch) fprintf(out, "%10u ", epoch); fprintf(out, "%s%s", p[i]->txt, p[i]->dead ? " DEAD" : ""); /* if the process is being watched */ if ((p[i]->tid == options.watch_pid) || (options.watch_name && options.show_cmdline && strstr(p[i]->cmdline, options.watch_name)) || (options.watch_name && !options.show_cmdline && strstr(p[i]->name, options.watch_name))) fprintf(out, " <---"); fprintf(out, "\n"); num_printed++; } } if (num_printed) fprintf(out, "\n"); fflush(out); if (options.command_done && options.sticky) break; if ((num_dead) && (!options.sticky)) compact_proc_list(proc_list); /* Wait some delay. Note that this syscall may be interrupted when we receive a signal, such as SICHLD. This is ok, it will force a refresh. */ select(0, NULL, NULL, NULL, &tv); /* prepare for next select */ tv.tv_sec = options.delay; tv.tv_usec = (options.delay - tv.tv_sec) * 1000000.0; } free(header); }