int main (int argc, char *argv[]) { guint64 block_size = 512, area_size = 1024 * 1024, n_blocks, repeats = 1000000; if (argc > 1) block_size = parse_memsize (argv[1]); else { usage(); block_size = 512; } if (argc > 2) area_size = parse_memsize (argv[2]); if (argc > 3) repeats = parse_memsize (argv[3]); if (argc > 4) g_slice_set_config (G_SLICE_CONFIG_COLOR_INCREMENT, parse_memsize (argv[4])); /* figure number of blocks from block and area size. * divide area by 3 because touch_mem() allocates 3 areas */ n_blocks = area_size / 3 / ALIGN (block_size, sizeof (gsize) * 2); /* basic sanity checks */ if (!block_size || !n_blocks || block_size >= area_size) { g_printerr ("Invalid arguments: block-size=%" G_GUINT64_FORMAT " memory-size=%" G_GUINT64_FORMAT "\n", block_size, area_size); usage(); return 1; } g_printerr ("Will allocate and touch %" G_GUINT64_FORMAT " blocks of %" G_GUINT64_FORMAT " bytes (= %" G_GUINT64_FORMAT " bytes) %" G_GUINT64_FORMAT " times with color increment: 0x%08" G_GINT64_MODIFIER "x\n", n_blocks, block_size, n_blocks * block_size, repeats, (guint64)g_slice_get_config (G_SLICE_CONFIG_COLOR_INCREMENT)); touch_mem (block_size, n_blocks, repeats); return 0; }
int main(int argc, char **argv) { struct sysinfo info; struct sigaction mysig; int i,rv=0; float duration_f, loops_per_sec; unsigned long free_mem, mapsize; basename=strrchr(argv[0],'/'); if (basename) basename++; else basename=argv[0]; /* Calculate default values */ /* Get processor count. */ num_cpus = sysconf(_SC_NPROCESSORS_CONF); /* Ensure we have at least two threads per CPU */ if (num_cpus*2 > default_threads) default_threads = num_cpus*2; /* Get memory info */ if (sysinfo(&info) != 0) { perror("sysinfo"); return -1; } free_mem=(info.freeram+info.bufferram)*info.mem_unit; total_ram=info.totalram*info.mem_unit; /* default to using most of free_mem */ default_memsize = free_mem * DEFAULT_MEMPCT; /* Set configurable values to reasonable defaults */ runtime = default_runtime; num_threads = default_threads; memsize = default_memsize; /* parse options */ while ((i = getopt(argc,argv,"hvqpt:n:m:")) != -1) { switch (i) { case 'h': usage(); return 0; case 'v': verbose=1; break; case 'q': quiet=1; break; case 'p': parallel=1; break; case 't': runtime=atoi(optarg); if (!runtime) { printf("%s: error: bad runtime \"%s\"\n",basename,optarg); return 1; } break; case 'n': num_threads=atoi(optarg); if (!num_threads) { printf("%s: error: bad thread count \"%s\"\n",basename,optarg); return 1; } break; case 'm': memsize=parse_memsize(optarg); if (!memsize) { printf("%s: error: bad memory size \"%s\"\n",basename,optarg); return 1; } break; } } /* calculate mapsize now that memsize/num_threads is set */ mapsize = memsize/num_threads; /* sanity checks */ if (num_threads < num_cpus) printf("Warning: num_threads < num_cpus. This isn't usually a good idea.\n"); if (memsize > free_mem) printf("Warning: memsize > free_mem. You will probably hit swap.\n"); /* A little information */ if (verbose) { printf("Detected %u processors.\n",num_cpus); printf("RAM: %.1f%% free (%s/", 100.0*(double)free_mem/(double)total_ram, human_memsize(free_mem)); printf("%s)\n",human_memsize(total_ram)); } printf("Testing %s RAM for %u seconds using %u threads:\n", human_memsize(memsize),runtime,num_threads); /* Allocate room for thread info */ threads=(pthread_t *)malloc(num_threads*sizeof(pthread_t)); mmap_regions=(char **)malloc(num_threads*sizeof(char *)); loop_counters=(unsigned long *)malloc(num_threads*sizeof(unsigned long *)); /* Create all our threads! */ while (created_threads < num_threads) { pthread_mutex_lock(&mmap_mutex); mmap_done=0; if (pthread_create(&threads[created_threads],NULL, mem_twiddler,(void*)&mapsize) != 0) { perror("pthread_create"); exit(1); } /* Wait for it to finish initializing */ while (!mmap_done) { pthread_cond_wait(&mmap_cond,&mmap_mutex); } pthread_mutex_unlock(&mmap_mutex); if (!verbose && !quiet) progressbar("Starting threads",created_threads,num_threads); } if (parallel) { /* Wait for the signal that everyone is finished initializing */ pthread_mutex_lock(&init_mutex); while (live_threads < num_threads) { pthread_cond_wait(&init_cond,&init_mutex); } pthread_mutex_unlock(&init_mutex); } /* Let the testing begin! */ if (!verbose && !quiet) printf("\n"); gettimeofday(&start,NULL); pthread_cond_broadcast(&test_start); /* catch ^C signal */ mysig.sa_handler=int_handler; sigemptyset(&mysig.sa_mask); mysig.sa_flags=0; sigaction(SIGINT,&mysig,NULL); /* Wait for the allotted time */ i=0; while (!done && (i<runtime)) { if (sleep(1) == 0) i++; if (!quiet) progressbar("Testing RAM",i,runtime); } if (i != runtime) rv=1; /* Signal completion and join all threads */ done=1; while (live_threads) { pthread_join(threads[live_threads-1],NULL); live_threads--; } gettimeofday(&finish,NULL); if (!quiet) printf("\n"); /* Test is officially complete. Calculate run speed. */ timersub(&finish,&start,&duration); duration_f=(float)duration.tv_sec + (float)duration.tv_usec / 1000000.0; loops_per_sec=0; if (verbose) printf("Runtime was %.2fs\n",duration_f); for (i=0;i<num_threads;i++) { if (verbose) printf("thread %i: %lu loops\n",i,loop_counters[i]); loops_per_sec += (float)loop_counters[i]/duration_f; } printf("Total loops per second: %.2f\n",loops_per_sec); /* All done. Return success. */ printf("Testing complete.\n"); return rv; }
int main(int argc, char* argv[]) { if (argc != 2) { fprintf(stderr, "[-] Error: usage: %s config_file\n", argv[0]); return EXIT_FAILURE; } char* filename = argv[1]; FILE* fp = fopen(filename, "r"); if (!fp) { fprintf(stderr, "[-] Error: failed to open file %s\n", filename); return EXIT_FAILURE; } /* File buffer */ char* buffer; int nbytes = 128; buffer = (char*)malloc(2048*sizeof(char)); /* Parsing vars */ int m_size; int p_id; int p_size; int r_page; int r_id; int w_page; int w_id; int e_id; init_process_table(); /* Main loop */ while ((getline(&buffer, (size_t*)&nbytes, fp) != -1)) { /* Todas as operações começam com letras diferentes */ switch(buffer[0]) { case 'M': /* MEMSIZE SIZE */ case 'm': parse_memsize(buffer, &m_size); printf("[+] MEMSIZE %d\n", m_size); memsize(m_size); break; case 'P': /* PROCSIZE ID SIZE */ case 'p': parse_procsize(buffer, &p_id, &p_size); printf("[+] PROCSIZE %d %d\n", p_id, p_size); procsize(p_id, p_size); break; case 'R': /* READ PAGE ID */ case 'r': parse_read(buffer, &r_page, &r_id); printf("[+] READ %d %d\n", r_page, r_id); read_p(r_page, r_id); break; case 'W': /* WRITE PAGE ID */ case 'w': parse_write(buffer, &w_page, &w_id); printf("[+] WRITE %d %d\n", w_page, w_id); write_p(w_page, w_id); break; case 'E': /* ENDPROC ID */ case 'e': parse_endproc(buffer, &e_id); printf("[+] ENDPROC %d\n", e_id); endproc(e_id); break; default: printf("[-] Invalid Operation!\n"); } } /* Write stats here */ write_stats(); fclose(fp); return EXIT_SUCCESS; }