Intel(R) Threading Building Blocks Doxygen Documentation  version 4.2.3
tbb_statistics.cpp
Go to the documentation of this file.
1 /*
2  Copyright (c) 2005-2019 Intel Corporation
3 
4  Licensed under the Apache License, Version 2.0 (the "License");
5  you may not use this file except in compliance with the License.
6  You may obtain a copy of the License at
7 
8  http://www.apache.org/licenses/LICENSE-2.0
9 
10  Unless required by applicable law or agreed to in writing, software
11  distributed under the License is distributed on an "AS IS" BASIS,
12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  See the License for the specific language governing permissions and
14  limitations under the License.
15 
16 
17 
18 
19 */
20 
21 #include "tbb_statistics.h"
22 
23 #if __TBB_STATISTICS
24 
25 #include <climits>
26 #include <cstdarg>
27 #if __TBB_STATISTICS_STDOUT
28 #include <cstdio>
29 #endif
30 
31 #include "tbb/spin_mutex.h"
32 
33 namespace tbb {
34 namespace internal {
35 
37 
39 const char* StatGroupTitles[] = {
40  "task objects", "tasks executed", "stealing attempts", "task proxies", "arena", "market", "priority ops", "prio ops details"
41 };
42 
44 
46 const char* StatFieldTitles[] = {
47  /*task objects*/ "active", "freed", "big", NULL,
48  /*tasks executed*/ "total", "w/o spawn", NULL,
49  /*stealing attempts*/ "succeeded", "failed", "conflicts", "backoffs", NULL,
50  /*task proxies*/ "mailed", "revoked", "stolen", "bypassed", "ignored", NULL,
51  /*arena*/ "switches", "roundtrips", "avg.conc", "avg.allot", NULL,
52  /*market*/ "roundtrips", NULL,
53  /*priority ops*/ "ar.switch", "mkt.switch", "ar.reset", "ref.fixup", "avg.ar.pr", "avg.mkt.pr", NULL,
54  /*prio ops details*/ "winnows", "reloads", "orphaned", "winnowed", "reloaded", NULL
55 };
56 
58 
60 class statistics_logger {
61 public:
62  statistics_logger () {
63  __TBB_ASSERT( sg_end - 1 == 1 << (sizeof(StatGroupTitles)/sizeof(*StatGroupTitles) - 1), NULL );
64 
65  my_file = fopen("statistics.txt","w");
66  if( !my_file )
67  perror("fopen(\"statistics.txt\"\")");
68  // Initialize groups dump layout info
69  group_start_field[0] = 0;
70  for ( size_t i = 0, j = 0; i < NumGroups; ++i, ++j ) {
71  __TBB_ASSERT( StatFieldTitles[j], "Empty group occurred" );
72  while ( StatFieldTitles[j] )
73  ++j;
74  group_start_field[i + 1] = j - i; // -i accounts for preceding NULL separators
75  }
76  __TBB_ASSERT( group_start_field[NumGroups] == statistics_counters::size(),
77  "Wrong number of elements in StatFieldTitles" );
78  dump( "\n%-*s", IDColumnWidth, "");
79  process_groups( &statistics_logger::print_group_title );
80  dump( "%-*s", IDColumnWidth, "ID");
81  process_groups( &statistics_logger::print_field_titles );
82  }
83 
84  ~statistics_logger () { fclose(my_file); }
85 
86  void record( const statistics_counters& c, size_t id ) {
87  spin_mutex::scoped_lock lock(my_mutex);
88  counters_to_dump = &c;
89 #if __TBB_STATISTICS_TOTALS_ONLY
90  if ( id == arena_counters_total ) {
91  dump( "%-*s", IDColumnWidth, "Tot" );
92  process_groups( &statistics_logger::print_field_values );
93  }
94 #else /* !__TBB_STATISTICS_TOTALS_ONLY */
95  const char* idString = NULL;
96  switch ( id ) {
97  case 0:
98  idString = "M"; break;
99  case workers_counters_total:
100  idString = "Wtot"; break;
101  case arena_counters_total:
102  idString = "Tot"; break;
103  default:
104  dump( "W%-*u", IDColumnWidth - 1, id );
105  }
106  if ( idString )
107  dump( "%-*s", IDColumnWidth, idString );
108  process_groups( &statistics_logger::print_field_values );
109 #endif /* !__TBB_STATISTICS_TOTALS_ONLY */
110  }
111 private:
112  static const size_t IDColumnWidth = 5;
113  static const size_t StatisticsColumnWidth = 10;
114  static const size_t NumGroups = sizeof(StatGroupTitles)/sizeof(char*);
115 
117  FILE* my_file;
119  spin_mutex my_mutex;
121 
122  size_t group_start_field[NumGroups + 1];
124  const statistics_counters* counters_to_dump;
125 
126  static const size_t NumFields = sizeof(StatFieldTitles)/sizeof(*StatFieldTitles) - NumGroups;
127  bool averages_fields[NumFields];
128 
129  void dump ( char const* fmt, ... ) {
130  va_list args;
131  if ( my_file ) {
132  va_start( args, fmt );
133  vfprintf( my_file, fmt, args );
134  va_end( args );
135  }
136 #if __TBB_STATISTICS_STDOUT
137  va_start( args, fmt );
138  vprintf( fmt, args );
139  va_end( args );
140 #endif
141  }
142 
143  void process_groups ( void (statistics_logger::*per_group_action)(size_t group_idx) ) {
144  for ( size_t i = 0, group_flag = 1; i < NumGroups; ++i, group_flag <<= 1 ) {
145  __TBB_ASSERT( group_flag < sg_end, "StatGroupTitles contents is incompatible with statistics_groups definition" );
146  if ( __TBB_ActiveStatisticsGroups & group_flag )
147  (this->*per_group_action)( i );
148  }
149  dump( "\n" );
150  }
151 
152  void print_group_title ( size_t group_idx ) {
153  dump( "%-*s", (group_start_field[group_idx + 1] - group_start_field[group_idx]) * (StatisticsColumnWidth + 1),
154  StatGroupTitles[group_idx] );
155  }
156 
157  void print_field_titles ( size_t group_idx ) {
158  // +group_idx accounts for preceding NULL separators
159  size_t i = group_start_field[group_idx] + group_idx;
160  while ( StatFieldTitles[i] ) {
161  averages_fields[i - group_idx] = strncmp(StatFieldTitles[i], "avg.", 4) == 0;
162  dump( "%-*s ", StatisticsColumnWidth, StatFieldTitles[i++] );
163  }
164  }
165 
166  void print_field_values ( size_t group_idx ) {
167  size_t begin = group_start_field[group_idx],
168  end = group_start_field[group_idx + 1];
169  for ( size_t i = begin; i < end; ++i ) {
170  if ( averages_fields[i] )
171  dump( "%-*.2f ", StatisticsColumnWidth, (double)counters_to_dump->field(i)/counters_to_dump->tasks_executed );
172  else
173  dump( "%-*ld ", StatisticsColumnWidth, counters_to_dump->field(i) );
174  }
175  }
176 }; // class statistics_logger
177 
178 static statistics_logger the_statistics;
179 
180 void dump_statistics ( const statistics_counters& c, size_t id ) {
181  the_statistics.record(c, id);
182 }
183 
184 } // namespace internal
185 } // namespace tbb
186 
187 #endif /* __TBB_STATISTICS */
void const char const char int ITT_FORMAT __itt_group_sync x void const char ITT_FORMAT __itt_group_sync s void ITT_FORMAT __itt_group_sync p void ITT_FORMAT p void ITT_FORMAT p no args __itt_suppress_mode_t unsigned int void size_t ITT_FORMAT d void ITT_FORMAT p void ITT_FORMAT p __itt_model_site __itt_model_site_instance ITT_FORMAT p __itt_model_task __itt_model_task_instance ITT_FORMAT p void ITT_FORMAT p void ITT_FORMAT p void size_t ITT_FORMAT d void ITT_FORMAT p const wchar_t ITT_FORMAT s const char ITT_FORMAT s const char ITT_FORMAT s const char ITT_FORMAT s no args void ITT_FORMAT p size_t ITT_FORMAT d no args const wchar_t const wchar_t ITT_FORMAT s __itt_heap_function void size_t int ITT_FORMAT d __itt_heap_function void ITT_FORMAT p __itt_heap_function void void size_t int ITT_FORMAT d no args no args unsigned int ITT_FORMAT u const __itt_domain __itt_id ITT_FORMAT lu const __itt_domain __itt_id __itt_id __itt_string_handle ITT_FORMAT p const __itt_domain __itt_id ITT_FORMAT p const __itt_domain __itt_id __itt_timestamp __itt_timestamp end
#define __TBB_ASSERT(predicate, comment)
No-op version of __TBB_ASSERT.
Definition: tbb_stddef.h:169
void const char const char int ITT_FORMAT __itt_group_sync x void const char ITT_FORMAT __itt_group_sync s void ITT_FORMAT __itt_group_sync p void ITT_FORMAT p void ITT_FORMAT p no args __itt_suppress_mode_t unsigned int void size_t ITT_FORMAT d void ITT_FORMAT p void ITT_FORMAT p __itt_model_site __itt_model_site_instance ITT_FORMAT p __itt_model_task __itt_model_task_instance ITT_FORMAT p void ITT_FORMAT p void ITT_FORMAT p void size_t ITT_FORMAT d void ITT_FORMAT p const wchar_t ITT_FORMAT s const char ITT_FORMAT s const char ITT_FORMAT s const char ITT_FORMAT s no args void ITT_FORMAT p size_t ITT_FORMAT d no args const wchar_t const wchar_t ITT_FORMAT s __itt_heap_function void size_t int ITT_FORMAT d __itt_heap_function void ITT_FORMAT p __itt_heap_function void void size_t int ITT_FORMAT d no args no args unsigned int ITT_FORMAT u const __itt_domain __itt_id ITT_FORMAT lu const __itt_domain __itt_id __itt_id __itt_string_handle ITT_FORMAT p const __itt_domain __itt_id ITT_FORMAT p const __itt_domain __itt_id __itt_timestamp begin
friend class scoped_lock
Definition: spin_mutex.h:180
void const char const char int ITT_FORMAT __itt_group_sync x void const char ITT_FORMAT __itt_group_sync s void ITT_FORMAT __itt_group_sync p void ITT_FORMAT p void ITT_FORMAT p no args __itt_suppress_mode_t unsigned int void size_t ITT_FORMAT d void ITT_FORMAT p void ITT_FORMAT p __itt_model_site __itt_model_site_instance ITT_FORMAT p __itt_model_task __itt_model_task_instance ITT_FORMAT p void * lock
The graph class.
void const char const char int ITT_FORMAT __itt_group_sync x void const char ITT_FORMAT __itt_group_sync s void ITT_FORMAT __itt_group_sync p void ITT_FORMAT p void ITT_FORMAT p no args __itt_suppress_mode_t unsigned int void size_t size

Copyright © 2005-2019 Intel Corporation. All Rights Reserved.

Intel, Pentium, Intel Xeon, Itanium, Intel XScale and VTune are registered trademarks or trademarks of Intel Corporation or its subsidiaries in the United States and other countries.

* Other names and brands may be claimed as the property of others.