1 // Copyright (c) 2005, Google Inc.
2 // All rights reserved.
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are
8 // * Redistributions of source code must retain the above copyright
9 // notice, this list of conditions and the following disclaimer.
10 // * Redistributions in binary form must reproduce the above
11 // copyright notice, this list of conditions and the following disclaimer
12 // in the documentation and/or other materials provided with the
14 // * Neither the name of Google Inc. nor the names of its
15 // contributors may be used to endorse or promote products derived from
16 // this software without specific prior written permission.
18 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 // Author: Craig Silverstein
33 // Does some simple arithmetic and a few libc routines, so we can profile it.
34 // Define WITH_THREADS to add pthread functionality as well (otherwise, btw,
35 // the num_threads argument to this program is ingored).
37 #include "config_for_unittests.h"
41 #include <unistd.h> // for fork()
43 #include <sys/wait.h> // for wait()
44 #include "gperftools/profiler.h"
45 #include "base/simple_mutex.h"
46 #include "tests/testutil.h"
48 static int result
= 0;
49 static int g_iters
= 0; // argv[1]
51 Mutex
mutex(Mutex::LINKER_INITIALIZED
);
53 static void test_other_thread() {
55 ProfilerRegisterThread();
60 for (m
= 0; m
< 1000000; ++m
) { // run millions of times
61 for (i
= 0; i
< g_iters
; ++i
) {
64 snprintf(b
, sizeof(b
), "other: %d", result
); // get some libc action
69 static void test_main_thread() {
73 for (m
= 0; m
< 1000000; ++m
) { // run millions of times
74 for (i
= 0; i
< g_iters
; ++i
) {
77 snprintf(b
, sizeof(b
), "same: %d", result
); // get some libc action
81 int main(int argc
, char** argv
) {
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");
85 fprintf(stderr
, " num_threads: how many concurrent threads.\n");
86 fprintf(stderr
, " 0 or 1 for single-threaded mode,\n");
87 fprintf(stderr
, " -# to fork instead of thread.\n");
88 fprintf(stderr
, " filename: The name of the output profile.\n");
89 fprintf(stderr
, (" If you don't specify, set CPUPROFILE "
90 "in the environment instead!\n"));
94 g_iters
= atoi(argv
[1]);
96 const char* filename
= NULL
;
98 num_threads
= atoi(argv
[2]);
105 ProfilerStart(filename
);
110 ProfilerFlush(); // just because we can
112 // The other threads, if any, will run only half as long as the main thread
113 RunManyThreads(test_other_thread
, num_threads
);
115 // Or maybe they asked to fork. The fork test is only interesting
116 // when we use CPUPROFILE to name, so check for that
118 for (; num_threads
< 0; ++num_threads
) { // -<num_threads> to fork
120 printf("FORK test only makes sense when no filename is specified.\n");
125 printf("FORK failed!\n");
128 return execl(argv
[0], argv
[0], argv
[1], NULL
);
130 wait(NULL
); // we'll let the kids run one at a time