Intel(R) Threading Building Blocks Doxygen Documentation version 4.2.3
Loading...
Searching...
No Matches
tbb_statistics.cpp
Go to the documentation of this file.
1/*
2 Copyright (c) 2005-2020 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#include "tbb_statistics.h"
18
19#if __TBB_STATISTICS
20
21#include <climits>
22#include <cstdarg>
23#if __TBB_STATISTICS_STDOUT
24#include <cstdio>
25#endif
26
27#include "tbb/spin_mutex.h"
28
29namespace tbb {
30namespace internal {
31
33
35const char* StatGroupTitles[] = {
36 "task objects", "tasks executed", "stealing attempts", "task proxies", "arena", "market", "priority ops", "prio ops details"
37};
38
40
42const char* StatFieldTitles[] = {
43 /*task objects*/ "active", "freed", "big", NULL,
44 /*tasks executed*/ "total", "w/o spawn", NULL,
45 /*stealing attempts*/ "succeeded", "failed", "conflicts", "backoffs", NULL,
46 /*task proxies*/ "mailed", "revoked", "stolen", "bypassed", "ignored", NULL,
47 /*arena*/ "switches", "roundtrips", "avg.conc", "avg.allot", NULL,
48 /*market*/ "roundtrips", NULL,
49 /*priority ops*/ "ar.switch", "mkt.switch", "ar.reset", "ref.fixup", "avg.ar.pr", "avg.mkt.pr", NULL,
50 /*prio ops details*/ "winnows", "reloads", "orphaned", "winnowed", "reloaded", NULL
51};
52
54
56class statistics_logger {
57public:
58 statistics_logger () {
59 __TBB_ASSERT( sg_end - 1 == 1 << (sizeof(StatGroupTitles)/sizeof(*StatGroupTitles) - 1), NULL );
60
61 my_file = fopen("statistics.txt","w");
62 if( !my_file )
63 perror("fopen(\"statistics.txt\"\")");
64 // Initialize groups dump layout info
65 group_start_field[0] = 0;
66 for ( size_t i = 0, j = 0; i < NumGroups; ++i, ++j ) {
67 __TBB_ASSERT( StatFieldTitles[j], "Empty group occurred" );
68 while ( StatFieldTitles[j] )
69 ++j;
70 group_start_field[i + 1] = j - i; // -i accounts for preceding NULL separators
71 }
72 __TBB_ASSERT( group_start_field[NumGroups] == statistics_counters::size(),
73 "Wrong number of elements in StatFieldTitles" );
74 dump( "\n%-*s", IDColumnWidth, "");
75 process_groups( &statistics_logger::print_group_title );
76 dump( "%-*s", IDColumnWidth, "ID");
77 process_groups( &statistics_logger::print_field_titles );
78 }
79
80 ~statistics_logger () { fclose(my_file); }
81
82 void record( const statistics_counters& c, size_t id ) {
83 spin_mutex::scoped_lock lock(my_mutex);
84 counters_to_dump = &c;
85#if __TBB_STATISTICS_TOTALS_ONLY
86 if ( id == arena_counters_total ) {
87 dump( "%-*s", IDColumnWidth, "Tot" );
88 process_groups( &statistics_logger::print_field_values );
89 }
90#else /* !__TBB_STATISTICS_TOTALS_ONLY */
91 const char* idString = NULL;
92 switch ( id ) {
93 case 0:
94 idString = "M"; break;
95 case workers_counters_total:
96 idString = "Wtot"; break;
97 case arena_counters_total:
98 idString = "Tot"; break;
99 default:
100 dump( "W%-*u", IDColumnWidth - 1, id );
101 }
102 if ( idString )
103 dump( "%-*s", IDColumnWidth, idString );
104 process_groups( &statistics_logger::print_field_values );
105#endif /* !__TBB_STATISTICS_TOTALS_ONLY */
106 }
107private:
108 static const size_t IDColumnWidth = 5;
109 static const size_t StatisticsColumnWidth = 10;
110 static const size_t NumGroups = sizeof(StatGroupTitles)/sizeof(char*);
111
113 FILE* my_file;
115 spin_mutex my_mutex;
117
118 size_t group_start_field[NumGroups + 1];
120 const statistics_counters* counters_to_dump;
121
122 static const size_t NumFields = sizeof(StatFieldTitles)/sizeof(*StatFieldTitles) - NumGroups;
123 bool averages_fields[NumFields];
124
125 void dump ( char const* fmt, ... ) {
126 va_list args;
127 if ( my_file ) {
128 va_start( args, fmt );
129 vfprintf( my_file, fmt, args );
130 va_end( args );
131 }
132#if __TBB_STATISTICS_STDOUT
133 va_start( args, fmt );
134 vprintf( fmt, args );
135 va_end( args );
136#endif
137 }
138
139 void process_groups ( void (statistics_logger::*per_group_action)(size_t group_idx) ) {
140 for ( size_t i = 0, group_flag = 1; i < NumGroups; ++i, group_flag <<= 1 ) {
141 __TBB_ASSERT( group_flag < sg_end, "StatGroupTitles contents is incompatible with statistics_groups definition" );
142 if ( __TBB_ActiveStatisticsGroups & group_flag )
143 (this->*per_group_action)( i );
144 }
145 dump( "\n" );
146 }
147
148 void print_group_title ( size_t group_idx ) {
149 dump( "%-*s", (group_start_field[group_idx + 1] - group_start_field[group_idx]) * (StatisticsColumnWidth + 1),
150 StatGroupTitles[group_idx] );
151 }
152
153 void print_field_titles ( size_t group_idx ) {
154 // +group_idx accounts for preceding NULL separators
155 size_t i = group_start_field[group_idx] + group_idx;
156 while ( StatFieldTitles[i] ) {
157 averages_fields[i - group_idx] = strncmp(StatFieldTitles[i], "avg.", 4) == 0;
158 dump( "%-*s ", StatisticsColumnWidth, StatFieldTitles[i++] );
159 }
160 }
161
162 void print_field_values ( size_t group_idx ) {
163 size_t begin = group_start_field[group_idx],
164 end = group_start_field[group_idx + 1];
165 for ( size_t i = begin; i < end; ++i ) {
166 if ( averages_fields[i] )
167 dump( "%-*.2f ", StatisticsColumnWidth, (double)counters_to_dump->field(i)/counters_to_dump->tasks_executed );
168 else
169 dump( "%-*ld ", StatisticsColumnWidth, counters_to_dump->field(i) );
170 }
171 }
172}; // class statistics_logger
173
174static statistics_logger the_statistics;
175
176void dump_statistics ( const statistics_counters& c, size_t id ) {
177 the_statistics.record(c, id);
178}
179
180} // namespace internal
181} // namespace tbb
182
183#endif /* __TBB_STATISTICS */
#define __TBB_ASSERT(predicate, comment)
No-op version of __TBB_ASSERT.
Definition tbb_stddef.h:165
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
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
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
The graph class.

Copyright © 2005-2020 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.