Line data Source code
1 : // This file (example_main.cc) was created by Ron Rechenmacher <ron@fnal.gov> on
2 : // Mar 12, 2014. "TERMS AND CONDITIONS" governing this file are in the README
3 : // or COPYING file. If you do not have such a file, one can be obtained by
4 : // contacting Ron or Fermi Lab in Batavia IL, 60510, phone: 630-840-3000.
5 : // $RCSfile: example_main.cc,v $
6 : // rev="$Revision: 1784 $$Date: 2026-10-09 13:51:46 -0500 (Fri, 09 Oct 2026) $";
7 :
8 : #define TRACE_DEFINE
9 : #include "TRACE/trace.h"
10 :
11 : #include <pthread.h> // pthread_self
12 : #include <sys/time.h> // gettimeofday
13 : #include <libgen.h> // basename (Darwin)
14 :
15 : #define USAGE \
16 : "\
17 : usage: %s\n\
18 : ", \
19 : basename(argv[0])
20 :
21 : void example_sub1(void);
22 :
23 : #define NUMTHREADS 3
24 : static int trace_thread_option= 0;
25 :
26 0 : void *thread_func(void *arg)
27 : {
28 0 : long loops= (long)arg;
29 0 : char tmp[PATH_MAX];
30 0 : long tid;
31 0 : timeval mark;
32 0 : if (trace_thread_option == 1) { // IF -std=c11 is NOT used, a seg fault usually occurs if default file does not exit
33 0 : tid= (long)trace_gettid();
34 0 : snprintf(tmp, sizeof(tmp), "/tmp/trace_buffer_%ld", tid);
35 0 : TRACE_CNTL("file", tmp);
36 0 : } else if (trace_thread_option == 2) {
37 0 : tid= (long)trace_gettid();
38 0 : snprintf(tmp, sizeof(tmp), "T%ld", tid);
39 0 : printf("setting name to %s\n", tmp);
40 0 : TRACE_CNTL("name", tmp);
41 : }
42 :
43 0 : gettimeofday(&mark, NULL);
44 0 : while (loops-- > 0) {
45 0 : TRACE(TLVL_INFO, "loops=%ld", loops);
46 0 : example_sub1();
47 : }
48 0 : TRACE(TLVL_LOG, "mark: %ld%06ld", mark.tv_sec, (long)mark.tv_usec);
49 :
50 0 : pthread_exit(NULL);
51 : }
52 :
53 0 : int main(int argc, char *argv[])
54 : {
55 0 : extern char *optarg; // for getopt
56 0 : extern int optind; // for getopt
57 0 : int opt; // for how I use getopt
58 0 : unsigned ii;
59 0 : pthread_t *threads;
60 0 : unsigned num_threads= NUMTHREADS;
61 0 : unsigned long loops= 4;
62 :
63 0 : while ((opt= getopt(argc, argv, "?hn:f:x:")) != -1) {
64 0 : switch (opt) { // '?' is also what you get w/ "invalid option -- -"
65 0 : case '?':
66 0 : case 'h':
67 0 : printf(USAGE);
68 0 : exit(0);
69 0 : break;
70 0 : case 'n': setenv("TRACE_NAME", optarg, 1); break;
71 0 : case 'f': setenv("TRACE_FILE", optarg, 1); break;
72 0 : case 'x': trace_thread_option= (int)strtoul(optarg, NULL, 0); break;
73 : }
74 : }
75 :
76 0 : if ((argc - optind) >= 1) {
77 0 : loops= strtoul(argv[1], NULL, 0);
78 0 : printf("loops set to %lu\n", loops);
79 : }
80 0 : if ((argc - optind) == 2) {
81 0 : num_threads= (unsigned)strtoul(argv[2], NULL, 0);
82 0 : printf("num_threads set to %u\n", num_threads);
83 : }
84 0 : threads= (pthread_t *)malloc(num_threads * sizeof(pthread_t));
85 0 : TRACE(TLVL_INFO, "example_main calling example");
86 0 : for (ii= 0; ii < num_threads; ii++) {
87 0 : if (loops == num_threads) pthread_create(&(threads[ii]), NULL, thread_func, (void *)(unsigned long)ii);
88 : else
89 0 : pthread_create(&(threads[ii]), NULL, thread_func, (void *)loops);
90 : }
91 0 : for (ii= 0; ii < num_threads; ii++) { pthread_join(threads[ii], NULL); }
92 :
93 0 : TRACE(TLVL_INFO, "back to example_main");
94 0 : free(threads);
95 0 : return (0);
96 : } // main
|