int foo(int i) { apex_profiler_handle profiler = apex_start(APEX_FUNCTION_ADDRESS, &foo); int j = i * i; apex_stop(profiler); profiler = apex_resume(APEX_FUNCTION_ADDRESS, &foo); int k = j * j; apex_stop(profiler); return k; }
int main(int argc, char **argv) { apex_init(argv[0], 0, 1); num_threads = apex_hardware_concurrency(); total_iterations = num_threads * ITERATIONS; apex_set_throttle_concurrency(true); apex_set_throttle_energy(true); apex_setup_timer_throttling(APEX_FUNCTION_ADDRESS, &foo, APEX_MINIMIZE_ACCUMULATED, APEX_ACTIVE_HARMONY, 1000000); int original_cap = apex_get_thread_cap(); apex_profiler_handle p = apex_start(APEX_FUNCTION_ADDRESS, &main); printf("PID of this process: %d\n", getpid()); int i; pthread_t * thread = (pthread_t*)calloc(num_threads, sizeof(pthread_t)); int * ids = (int*)calloc(num_threads, sizeof(int)); for (i = 0 ; i < num_threads ; i++) { ids[i] = i; pthread_create(&(thread[i]), NULL, someThread, &(ids[i])); } for (i = 0 ; i < num_threads ; i++) { pthread_join(thread[i], NULL); } apex_stop(p); int final_cap = apex_get_thread_cap(); if (final_cap < original_cap) { printf ("Test passed.\n"); } apex_finalize(); free(thread); return(0); }
void* someThread(void* tmp) { int *myid = (int*)tmp; apex_register_thread("threadTest thread"); apex_profiler_handle p = apex_start(APEX_FUNCTION_ADDRESS, &someThread); #if defined (__APPLE__) //printf("The ID of this thread is: %lu\n", (unsigned long)pthread_self()); #else //printf("The ID of this thread is: %u\n", (unsigned int)pthread_self()); #endif printf("The scheduler ID of this thread: %d\n", *myid); while (total_iterations > 0) { if (*myid >= apex_get_thread_cap()) { apex_set_state(APEX_THROTTLED); //printf("Thread %d sleeping for a bit.\n", *myid); struct timespec tim, tim2; tim.tv_sec = 0; tim.tv_nsec = 1000000; // 1/1000 second // sleep a bit nanosleep(&tim , &tim2); apex_set_state(APEX_BUSY); } else { foo(total_iterations); __sync_fetch_and_sub(&(total_iterations),1); if (total_iterations % 1000 == 0) { printf("%d iterations left, cap is %d\n", total_iterations, apex_get_thread_cap()); } } } printf("Thread done: %d. Current Cap: %d.\n", *myid, apex_get_thread_cap()); apex_stop(p); apex_exit_thread(); return NULL; }
int func(int i) { char name[128]; sprintf(name, "func %d", i); apex_profiler_handle profiler = apex_start(APEX_NAME_STRING, name); int j = i * i; apex_stop(profiler); return j; }
int main (int argc, char** argv) { apex_init_args(argc, argv, "apex_reset unit test"); apex_set_use_screen_output(1); printf("APEX Version : %s\n", apex_version()); apex_set_node_id(0); apex_profiler_handle main_profiler = apex_start(APEX_FUNCTION_ADDRESS,(void*)(main)); int i = 0; // Call "foo" 30 times for(i = 0; i < 30; ++i) { apex_profiler_handle p = apex_start(APEX_NAME_STRING,"foo"); apex_stop(p); } // Call "bar" 40 times for(i = 0; i < 40; ++i) { apex_profiler_handle p = apex_start(APEX_NAME_STRING,"bar"); apex_stop(p); } // Reset everything apex_reset(APEX_FUNCTION_ADDRESS, APEX_NULL_FUNCTION_ADDRESS); usleep(100); // Call "foo" 3 times for(i = 0; i < 3; ++i) { apex_profiler_handle p = apex_start(APEX_NAME_STRING,"foo"); apex_stop(p); } // Call "bar" 4 times for(i = 0; i < 4; ++i) { apex_profiler_handle p = apex_start(APEX_NAME_STRING,"bar"); apex_stop(p); } // The profile should show "foo" was called 3 times // and bar was called 4 times. // Call "Test Timer" 100 times for(i = 0; i < 100; ++i) { apex_profiler_handle p = apex_start(APEX_NAME_STRING,"Test Timer"); apex_stop(p); } // Reset "Test Timer" apex_reset(APEX_NAME_STRING, "Test Timer"); usleep(100); // Call "Test Timer" 25 times for(i = 0; i < 25; ++i) { apex_profiler_handle p = apex_start(APEX_NAME_STRING,"Test Timer"); apex_stop(p); } // The profile should show "Test Timer" was called 25 times. apex_stop(main_profiler); apex_finalize(); apex_profile * profile = apex_get_profile(APEX_NAME_STRING,"Test Timer"); if (profile) { printf("Value Reported : %f\n", profile->calls); if (profile->calls <= 25) { // might be less, some calls might have been missed printf("Test passed.\n"); } } apex_cleanup(); return 0; }
int main (int argc, char** argv) { apex_init_args(argc, argv, "apex_start unit test"); apex_profiler_handle profiler = apex_start(APEX_FUNCTION_ADDRESS, &main); int i,j = 0; for (i = 0 ; i < 3 ; i++) { j += foo(i); } apex_stop(profiler); apex_finalize(); apex_cleanup(); return 0; }
uintptr_t foo(uintptr_t i) { int j = 0; apex_profiler_handle profiler = apex_start(APEX_FUNCTION_ADDRESS, &foo); int x,y; for (x = 0 ; x < MAX_OUTER ; x++) { for (y = 0 ; y < MAX_INNER ; y++) { j += func(x) * func(y) + i; } } apex_stop(profiler); return j; }
int main (int argc, char** argv) { apex_init("apex_resume unit test", 0, 1); apex_set_use_screen_output(1); apex_profiler_handle profiler = apex_start(APEX_FUNCTION_ADDRESS, &main); int i,j = 0; for (i = 0 ; i < 3 ; i++) j += foo(i); apex_stop(profiler); apex_finalize(); apex_cleanup(); return 0; }
int foo (int i) { apex_profiler_handle p = apex_start(APEX_FUNCTION_ADDRESS, &foo); int j = i*i; double randval = 1.0 + (((double)(rand())) / RAND_MAX); struct timespec tim, tim2; tim.tv_sec = 0; // sleep just a bit longer, based on number of active threads. int cap = MIN(num_threads,apex_get_thread_cap()); tim.tv_nsec = (unsigned long)(SLEEPY_TIME * randval * (cap * cap)); nanosleep(&tim , &tim2); apex_stop(p); return j; }
void* someThread(void* tmp) { UNUSED(tmp); apex_register_thread("threadTest thread"); apex_profiler_handle profiler = apex_start(APEX_FUNCTION_ADDRESS,(void*)&someThread); printf("PID of this process: %d\n", getpid()); #if defined (__APPLE__) printf("The ID of this thread is: %lu\n", (unsigned long)pthread_self()); #else printf("The ID of this thread is: %u\n", (unsigned int)pthread_self()); #endif apex_stop(profiler); apex_exit_thread(); return NULL; }
void* someThread(void* tmp) { apex_register_thread("threadTest thread"); apex_custom_event(custom_type_1, NULL); apex_sample_value("some value", 42); apex_profiler_handle my_profiler = apex_start(APEX_FUNCTION_ADDRESS, &someThread); int i = 0; for (i = 0 ; i < ITERATIONS ; i++) { foo(i); } apex_custom_event(custom_type_2, NULL); apex_stop(my_profiler); apex_exit_thread(); return NULL; }
int foo (int i) { static __thread apex_profiler_handle my_profiler; if (i % 2 == 0) { my_profiler = apex_start(APEX_FUNCTION_ADDRESS, &foo); } else { my_profiler = apex_resume(APEX_FUNCTION_ADDRESS, &foo); } int result = i*i; if (i % 2 == 0) { apex_yield(my_profiler); } else { apex_stop(my_profiler); } return result; }
int main(int argc, char **argv) { apex_init_args(argc, argv, "apex_exit_thread unit test"); apex_set_node_id(0); apex_profiler_handle profiler = apex_start(APEX_FUNCTION_ADDRESS,(void*)&main); pthread_t * thread = (pthread_t*)(malloc(sizeof(pthread_t) * NUM_THREADS)); int i; for (i = 0 ; i < NUM_THREADS ; i++) { pthread_create(&(thread[i]), NULL, someThread, NULL); } for (i = 0 ; i < NUM_THREADS ; i++) { pthread_join(thread[i], NULL); } apex_stop(profiler); apex_finalize(); return(0); }
void * fib (void * in) { apex_register_thread("fib thread"); apex_profiler_handle p = apex_start(APEX_FUNCTION_ADDRESS, &fib); scratchpad_t* scratch = (scratchpad_t*)(in); if (scratch->x == 0) { scratch->f_x = 0; apex_stop(p); apex_exit_thread(); pthread_exit(NULL); } else if (scratch->x == 1) { scratch->f_x = 1; apex_stop(p); apex_exit_thread(); pthread_exit(NULL); } scratchpad_t a; a.x = (scratch->x)-1; a.f_x = 0; pthread_attr_t attr_a; pthread_attr_init(&attr_a); //pthread_attr_setstacksize(&attr_a, PTHREAD_STACK_MIN); pthread_t thread_a; int rc_a = pthread_create(&thread_a, &attr_a, fib, (void*)&a); if (rc_a == EAGAIN) { //printf("Insufficient resources to create another thread. \n EAGAIN A system-imposed limit on the number of threads was encountered. There are a number of limits that may trigger this error: the RLIMIT_NPROC soft resource limit (set via setrlimit(2)), which limits the number of processes and threads for a real user ID, was reached; the kernel's system- wide limit on the number of processes and threads, /proc/sys/kernel/threads-max, was reached (see proc(5)); or the maximum number of PIDs, /proc/sys/kernel/pid_max, was reached (see proc(5))."); } else if (rc_a == EINVAL) { //printf("Invalid settings in attr."); } else if (rc_a == EPERM) { //printf("No permission to set the scheduling policy and parameters specified in attr."); } pthread_attr_destroy(&attr_a); scratchpad_t b; b.x = (scratch->x)-2; b.f_x = 0; pthread_attr_t attr_b; pthread_attr_init(&attr_b); //pthread_attr_setstacksize(&attr_b, PTHREAD_STACK_MIN); pthread_t thread_b; int rc_b = pthread_create(&thread_b,&attr_b,fib,(void*)&b); if (rc_b == EAGAIN) { //printf("Insufficient resources to create another thread. \n EAGAIN A system-imposed limit on the number of threads was encountered. There are a number of limits that may trigger this error: the RLIMIT_NPROC soft resource limit (set via setrlimit(2)), which limits the number of processes and threads for a real user ID, was reached; the kernel's system- wide limit on the number of processes and threads, /proc/sys/kernel/threads-max, was reached (see proc(5)); or the maximum number of PIDs, /proc/sys/kernel/pid_max, was reached (see proc(5))."); } else if (rc_b == EINVAL) { //printf("Invalid settings in attr."); } else if (rc_b == EPERM) { //printf("No permission to set the scheduling policy and parameters specified in attr."); } pthread_attr_destroy(&attr_a); if (rc_a > 0) { // thread failed, just execute. fib((void*)&a); //a.f_x == fib_results[a.x]; } else { pthread_join(thread_a,NULL); } if (rc_b > 0) { // thread failed, just execute. fib((void*)&b); //b.f_x == fib_results[b.x]; } else { pthread_join(thread_b,NULL); } if (a.f_x != fib_results[a.x]) { printf("WRONG! fib of %d is NOT %d (valid value: %d)\n", a.x, a.f_x, fib_results[a.x]); a.f_x = fib_results[a.x]; } if (b.f_x != fib_results[b.x]) { printf("WRONG! fib of %d is NOT %d (valid value: %d)\n", b.x, b.f_x, fib_results[b.x]); b.f_x = fib_results[b.x]; } scratch->f_x = a.f_x + b.f_x; apex_stop(p); apex_exit_thread(); pthread_exit(NULL); }
int main (int argc, char** argv) { apex_init("apex_get_idle_rate unit test", 0, 1); apex_set_use_screen_output(1); printf("APEX Version : %s\n", apex_version()); apex_profiler_handle main_profiler = apex_start(APEX_FUNCTION_ADDRESS,(void*)(main)); int i = 0; // Call "foo" 30 times for(i = 0; i < 30; ++i) { apex_profiler_handle p = apex_start(APEX_NAME_STRING,"foo"); apex_stop(p); } // Call "bar" 40 times for(i = 0; i < 40; ++i) { apex_profiler_handle p = apex_start(APEX_NAME_STRING,"bar"); apex_stop(p); } // suspend measurement apex_set_papi_suspend(true); usleep(100); // Call "foo" 3 times for(i = 0; i < 3; ++i) { apex_profiler_handle p = apex_start(APEX_NAME_STRING,"foo"); apex_stop(p); } // Call "bar" 4 times for(i = 0; i < 4; ++i) { apex_profiler_handle p = apex_start(APEX_NAME_STRING,"bar"); apex_stop(p); } // The profile should show "foo" was called 3 times // and bar was called 4 times. // Call "Test Timer" 100 times for(i = 0; i < 100; ++i) { apex_profiler_handle p = apex_start(APEX_NAME_STRING,"Test Timer"); apex_stop(p); } // resume measurement apex_set_papi_suspend(false); usleep(100); // Call "Test Timer" 25 times for(i = 0; i < 25; ++i) { apex_profiler_handle p = apex_start(APEX_NAME_STRING,"Test Timer"); apex_stop(p); } bool passed = true; // The profile should show special "APEX Idle Time" counter. apex_profile * profile = apex_get_profile(APEX_NAME_STRING,APEX_IDLE_TIME); if (profile) { printf("Idle time reported : %f\n", profile->accumulated); if (profile->accumulated >= 0.0) { // might be less, some calls might have been missed } else { passed = false; } } // The profile should show special "APEX Non-Idle Time" counter. profile = apex_get_profile(APEX_NAME_STRING,APEX_NON_IDLE_TIME); if (profile) { printf("Non-Idle time reported : %f\n", profile->accumulated); if (profile->accumulated >= 0.0) { // might be less, some calls might have been missed } else { passed = false; } } // The profile should show special "APEX Idle Rate" counter. profile = apex_get_profile(APEX_NAME_STRING,APEX_IDLE_RATE); if (profile) { printf("Idle rate reported : %f\n", profile->accumulated); if (profile->accumulated >= 0.0) { // might be less, some calls might have been missed } else { passed = false; } } apex_stop(main_profiler); // The profile should show special "APEX Idle Time" counter. profile = apex_get_profile(APEX_NAME_STRING,APEX_IDLE_TIME); if (profile) { printf("Idle time reported : %f\n", profile->accumulated); if (profile->accumulated >= 0.0) { // might be less, some calls might have been missed } else { passed = false; } } // The profile should show special "APEX Non-Idle Time" counter. profile = apex_get_profile(APEX_NAME_STRING,APEX_NON_IDLE_TIME); if (profile) { printf("Non-Idle time reported : %f\n", profile->accumulated); if (profile->accumulated >= 0.0) { // might be less, some calls might have been missed } else { passed = false; } } profile = apex_get_profile(APEX_NAME_STRING,APEX_IDLE_RATE); if (profile) { printf("Idle rate reported : %f\n", profile->accumulated); if (profile->accumulated >= 0.0) { // might be less, some calls might have been missed } else { passed = false; } } apex_finalize(); // The profile should show special "APEX Idle Time" counter. profile = apex_get_profile(APEX_NAME_STRING,APEX_IDLE_TIME); if (profile) { printf("Idle time reported : %f\n", profile->accumulated); if (profile->accumulated >= 0.0) { // might be less, some calls might have been missed } else { passed = false; } } // The profile should show special "APEX Non-Idle Time" counter. profile = apex_get_profile(APEX_NAME_STRING,APEX_NON_IDLE_TIME); if (profile) { printf("Non-Idle time reported : %f\n", profile->accumulated); if (profile->accumulated >= 0.0) { // might be less, some calls might have been missed } else { passed = false; } } profile = apex_get_profile(APEX_NAME_STRING,APEX_IDLE_RATE); if (profile) { printf("Idle rate reported : %f\n", profile->accumulated); if (profile->accumulated >= 0.0) { // might be less, some calls might have been missed } else { passed = false; } } profile = apex_get_profile(APEX_NAME_STRING,"Test Timer"); if (profile) { printf("Value Reported : %f\n", profile->calls); if (profile->calls > 25) { // might be less, some calls might have been missed passed = false; } } profile = apex_get_profile(APEX_NAME_STRING,"foo"); if (profile) { printf("Value Reported : %f\n", profile->calls); if (profile->calls > 30) { // might be less, some calls might have been missed passed = false; } } profile = apex_get_profile(APEX_NAME_STRING,"bar"); if (profile) { printf("Value Reported : %f\n", profile->calls); if (profile->calls > 40) { // might be less, some calls might have been missed passed = false; } } if (passed) { printf("Test passed.\n"); } apex_cleanup(); return 0; }
int main(int argc, char **argv) { apex_policy_handle * on_startup = apex_register_policy(APEX_STARTUP, startup_policy); apex_policy_handle * on_shutdown = apex_register_policy(APEX_SHUTDOWN, policy_event); apex_policy_handle * on_new_node = apex_register_policy(APEX_NEW_NODE, policy_event); apex_policy_handle * on_new_thread = apex_register_policy(APEX_NEW_THREAD, policy_event); apex_init_args(argc, argv, NULL); apex_set_node_id(0); apex_policy_handle * on_start_event = apex_register_policy(APEX_START_EVENT, policy_event); apex_policy_handle * on_stop_event = apex_register_policy(APEX_STOP_EVENT, policy_event); apex_policy_handle * on_resume_event = apex_register_policy(APEX_RESUME_EVENT, policy_event); apex_policy_handle * on_yield_event = apex_register_policy(APEX_YIELD_EVENT, policy_event); apex_policy_handle * on_sample_value = apex_register_policy(APEX_SAMPLE_VALUE, policy_event); custom_type_1 = apex_register_custom_event("CUSTOM 1"); custom_type_2 = apex_register_custom_event("CUSTOM 2"); apex_policy_handle * on_custom_event_1 = apex_register_policy(custom_type_1, policy_event); apex_policy_handle * on_custom_event_2 = apex_register_policy(custom_type_2, policy_event); apex_policy_handle * on_periodic = apex_register_periodic_policy(1000000, policy_periodic); apex_profiler_handle my_profiler = apex_start(APEX_FUNCTION_ADDRESS, &main); pthread_t thread[NUM_THREADS]; int i; for (i = 0 ; i < NUM_THREADS ; i++) { pthread_create(&(thread[i]), NULL, someThread, NULL); } for (i = 0 ; i < NUM_THREADS ; i++) { pthread_join(thread[i], NULL); } // now un-register the policies printf("Deregistering %d...\n", on_startup->id); printf("Deregistering %d...\n", on_shutdown->id); printf("Deregistering %d...\n", on_new_node->id); printf("Deregistering %d...\n", on_new_thread->id); printf("Deregistering %d...\n", on_start_event->id); printf("Deregistering %d...\n", on_stop_event->id); printf("Deregistering %d...\n", on_resume_event->id); printf("Deregistering %d...\n", on_yield_event->id); printf("Deregistering %d...\n", on_sample_value->id); printf("Deregistering %d...\n", on_custom_event_1->id); printf("Deregistering %d...\n", on_custom_event_2->id); printf("Deregistering %d...\n", on_periodic->id); apex_deregister_policy(on_startup); apex_deregister_policy(on_shutdown); apex_deregister_policy(on_new_node); apex_deregister_policy(on_new_thread); apex_deregister_policy(on_start_event); apex_deregister_policy(on_stop_event); apex_deregister_policy(on_resume_event); apex_deregister_policy(on_yield_event); apex_deregister_policy(on_sample_value); apex_deregister_policy(on_custom_event_1); apex_deregister_policy(on_custom_event_2); apex_deregister_policy(on_periodic); printf("Running without policies now...\n"); for (i = 0 ; i < NUM_THREADS ; i++) { pthread_create(&(thread[i]), NULL, someThread, NULL); } for (i = 0 ; i < NUM_THREADS ; i++) { pthread_join(thread[i], NULL); } apex_stop(my_profiler); apex_finalize(); return(0); }