| OLD | NEW |
| 1 // Copyright (c) 2005, Google Inc. | 1 // Copyright (c) 2005, Google Inc. |
| 2 // All rights reserved. | 2 // All rights reserved. |
| 3 // | 3 // |
| 4 // Redistribution and use in source and binary forms, with or without | 4 // Redistribution and use in source and binary forms, with or without |
| 5 // modification, are permitted provided that the following conditions are | 5 // modification, are permitted provided that the following conditions are |
| 6 // met: | 6 // met: |
| 7 // | 7 // |
| 8 // * Redistributions of source code must retain the above copyright | 8 // * Redistributions of source code must retain the above copyright |
| 9 // notice, this list of conditions and the following disclaimer. | 9 // notice, this list of conditions and the following disclaimer. |
| 10 // * Redistributions in binary form must reproduce the above | 10 // * Redistributions in binary form must reproduce the above |
| (...skipping 23 matching lines...) Expand all Loading... |
| 34 // Define WITH_THREADS to add pthread functionality as well (otherwise, btw, | 34 // Define WITH_THREADS to add pthread functionality as well (otherwise, btw, |
| 35 // the num_threads argument to this program is ingored). | 35 // the num_threads argument to this program is ingored). |
| 36 | 36 |
| 37 #include "config_for_unittests.h" | 37 #include "config_for_unittests.h" |
| 38 #include <stdio.h> | 38 #include <stdio.h> |
| 39 #include <stdlib.h> | 39 #include <stdlib.h> |
| 40 #ifdef HAVE_UNISTD_H | 40 #ifdef HAVE_UNISTD_H |
| 41 #include <unistd.h> // for fork() | 41 #include <unistd.h> // for fork() |
| 42 #endif | 42 #endif |
| 43 #include <sys/wait.h> // for wait() | 43 #include <sys/wait.h> // for wait() |
| 44 #include "google/profiler.h" | 44 #include "gperftools/profiler.h" |
| 45 #include "base/simple_mutex.h" | 45 #include "base/simple_mutex.h" |
| 46 #include "tests/testutil.h" | 46 #include "tests/testutil.h" |
| 47 | 47 |
| 48 static int result = 0; | 48 static int result = 0; |
| 49 static int g_iters = 0; // argv[1] | 49 static int g_iters = 0; // argv[1] |
| 50 | 50 |
| 51 Mutex mutex(Mutex::LINKER_INITIALIZED); | 51 Mutex mutex(Mutex::LINKER_INITIALIZED); |
| 52 | 52 |
| 53 static void test_other_thread() { | 53 static void test_other_thread() { |
| 54 #ifndef NO_THREADS | 54 #ifndef NO_THREADS |
| 55 ProfilerRegisterThread(); | 55 ProfilerRegisterThread(); |
| 56 | 56 |
| 57 int i, m; | 57 int i, m; |
| 58 char b[128]; | 58 char b[128]; |
| 59 MutexLock ml(&mutex); | 59 MutexLock ml(&mutex); |
| 60 for (m = 0; m < 1000000; ++m) { // run millions of times | 60 for (m = 0; m < 1000000; ++m) { // run millions of times |
| 61 for (i = 0; i < g_iters; ++i ) { | 61 for (i = 0; i < g_iters; ++i ) { |
| 62 result ^= i; | 62 result ^= i; |
| 63 } | 63 } |
| 64 snprintf(b, sizeof(b), "%d", result); // get some libc action | 64 snprintf(b, sizeof(b), "other: %d", result); // get some libc action |
| 65 } | 65 } |
| 66 #endif | 66 #endif |
| 67 } | 67 } |
| 68 | 68 |
| 69 static void test_main_thread() { | 69 static void test_main_thread() { |
| 70 int i, m; | 70 int i, m; |
| 71 char b[128]; | 71 char b[128]; |
| 72 MutexLock ml(&mutex); | 72 MutexLock ml(&mutex); |
| 73 for (m = 0; m < 1000000; ++m) { // run millions of times | 73 for (m = 0; m < 1000000; ++m) { // run millions of times |
| 74 for (i = 0; i < g_iters; ++i ) { | 74 for (i = 0; i < g_iters; ++i ) { |
| 75 result ^= i; | 75 result ^= i; |
| 76 } | 76 } |
| 77 snprintf(b, sizeof(b), "%d", result); // get some libc action | 77 snprintf(b, sizeof(b), "same: %d", result); // get some libc action |
| 78 } | 78 } |
| 79 } | 79 } |
| 80 | 80 |
| 81 int main(int argc, char** argv) { | 81 int main(int argc, char** argv) { |
| 82 if ( argc <= 1 ) { | 82 if ( argc <= 1 ) { |
| 83 fprintf(stderr, "USAGE: %s <iters> [num_threads] [filename]\n", argv[0]); | 83 fprintf(stderr, "USAGE: %s <iters> [num_threads] [filename]\n", argv[0]); |
| 84 fprintf(stderr, " iters: How many million times to run the XOR test.\n"); | 84 fprintf(stderr, " iters: How many million times to run the XOR test.\n"); |
| 85 fprintf(stderr, " num_threads: how many concurrent threads.\n"); | 85 fprintf(stderr, " num_threads: how many concurrent threads.\n"); |
| 86 fprintf(stderr, " 0 or 1 for single-threaded mode,\n"); | 86 fprintf(stderr, " 0 or 1 for single-threaded mode,\n"); |
| 87 fprintf(stderr, " -# to fork instead of thread.\n"); | 87 fprintf(stderr, " -# to fork instead of thread.\n"); |
| (...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 133 #endif | 133 #endif |
| 134 | 134 |
| 135 test_main_thread(); | 135 test_main_thread(); |
| 136 | 136 |
| 137 if (filename) { | 137 if (filename) { |
| 138 ProfilerStop(); | 138 ProfilerStop(); |
| 139 } | 139 } |
| 140 | 140 |
| 141 return 0; | 141 return 0; |
| 142 } | 142 } |
| OLD | NEW |