1 /*
2     Copyright (c) 2014-2016 Intel Corporation.  All Rights Reserved.
3 
4     Redistribution and use in source and binary forms, with or without
5     modification, are permitted provided that the following conditions
6     are met:
7 
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 copyright
11         notice, this list of conditions and the following disclaimer in the
12         documentation and/or other materials provided with the distribution.
13       * Neither the name of Intel Corporation nor the names of its
14         contributors may be used to endorse or promote products derived
15         from this software without specific prior written permission.
16 
17     THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18     "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19     LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20     A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21     HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22     SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23     LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24     DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25     THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26     (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27     OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 */
29 
30 
31 #ifndef OFFLOAD_TIMER_H_INCLUDED
32 #define OFFLOAD_TIMER_H_INCLUDED
33 
34 #include <stdio.h>
35 #include <stdarg.h>
36 #include <stdint.h>
37 #include "liboffload_error_codes.h"
38 
39 DLL_LOCAL extern int timer_enabled;
40 
41 #ifdef TIMING_SUPPORT
42 
43 struct OffloadTargetTimerData {
44     uint64_t frequency;
45     struct {
46         uint64_t start;
47         uint64_t total;
48     } phases[c_offload_target_max_phase];
49 };
50 
51 struct OffloadHostTimerData {
52     // source file name and line number
53     const char* file;
54     int         line;
55 
56     // host timer data
57     struct {
58         uint64_t start;
59         uint64_t total;
60     } phases[c_offload_host_max_phase];
61 
62     uint64_t sent_bytes;
63     uint64_t received_bytes;
64     int card_number;
65     int offload_number;
66 
67     // target timer data
68     OffloadTargetTimerData target;
69 
70     // next element
71     OffloadHostTimerData *next;
72 };
73 
74 #if HOST_LIBRARY
75 
76 DLL_LOCAL extern int offload_report_level;
77 DLL_LOCAL extern int offload_report_enabled;
78 #define OFFLOAD_REPORT_1 1
79 #define OFFLOAD_REPORT_2 2
80 #define OFFLOAD_REPORT_3 3
81 #define OFFLOAD_REPORT_ON 1
82 #define OFFLOAD_REPORT_OFF 0
83 
84 #define OFFLOAD_TIMER_DATALEN() \
85     ((timer_enabled || (offload_report_level && offload_report_enabled)) ? \
86      ((1 + c_offload_target_max_phase) * sizeof(uint64_t)) : 0)
87 
88 #define OFFLOAD_TIMER_START(timer_data, pnode) \
89     if (timer_enabled || \
90         (offload_report_level && offload_report_enabled)) { \
91         offload_timer_start(timer_data, pnode); \
92     }
93 
94 #define OFFLOAD_TIMER_STOP(timer_data, pnode) \
95     if (timer_enabled || \
96         (offload_report_level && offload_report_enabled)) { \
97         offload_timer_stop(timer_data, pnode); \
98     }
99 
100 #define OFFLOAD_TIMER_INIT(file, line) \
101     offload_timer_init(file, line);
102 
103 #define OFFLOAD_TIMER_TARGET_DATA(timer_data, data) \
104     if (timer_enabled || \
105         (offload_report_level && offload_report_enabled)) { \
106         offload_timer_fill_target_data(timer_data, data); \
107     }
108 
109 #define OFFLOAD_TIMER_HOST_SDATA(timer_data, data) \
110     if (offload_report_level && offload_report_enabled) { \
111         offload_timer_fill_host_sdata(timer_data, data); \
112     }
113 
114 #define OFFLOAD_TIMER_HOST_RDATA(timer_data, data) \
115     if (offload_report_level && offload_report_enabled) { \
116         offload_timer_fill_host_rdata(timer_data, data); \
117     }
118 
119 #define OFFLOAD_TIMER_HOST_MIC_NUM(timer_data, data) \
120     if (offload_report_level && offload_report_enabled) { \
121         offload_timer_fill_host_mic_num(timer_data, data); \
122     }
123 
124 extern DLL_LOCAL void offload_timer_start(OffloadHostTimerData *,
125                                 OffloadHostPhase t_node);
126 extern DLL_LOCAL void offload_timer_stop(OffloadHostTimerData *,
127                                OffloadHostPhase t_node);
128 extern DLL_LOCAL OffloadHostTimerData * offload_timer_init(const char *file, int line);
129 extern DLL_LOCAL void offload_timer_fill_target_data(OffloadHostTimerData *,
130                                            void *data);
131 extern DLL_LOCAL void offload_timer_fill_host_sdata(OffloadHostTimerData *,
132                                           uint64_t sent_bytes);
133 extern DLL_LOCAL void offload_timer_fill_host_rdata(OffloadHostTimerData *,
134                                           uint64_t sent_bytes);
135 extern DLL_LOCAL void offload_timer_fill_host_mic_num(OffloadHostTimerData *,
136                                             int card_number);
137 
138 // Utility structure for starting/stopping timer
139 struct OffloadTimer {
OffloadTimerOffloadTimer140     OffloadTimer(OffloadHostTimerData *data, OffloadHostPhase phase) :
141         m_data(data),
142         m_phase(phase)
143     {
144         OFFLOAD_TIMER_START(m_data, m_phase);
145     }
146 
~OffloadTimerOffloadTimer147     ~OffloadTimer()
148     {
149         OFFLOAD_TIMER_STOP(m_data, m_phase);
150     }
151 
152 private:
153     OffloadHostTimerData*   m_data;
154     OffloadHostPhase        m_phase;
155 };
156 
157 #else
158 
159 #define OFFLOAD_TIMER_DATALEN() \
160     ((timer_enabled) ? \
161      ((1 + c_offload_target_max_phase) * sizeof(uint64_t)) : 0)
162 
163 #define OFFLOAD_TIMER_START(pnode) \
164     if (timer_enabled) offload_timer_start(pnode);
165 
166 #define OFFLOAD_TIMER_STOP(pnode) \
167     if (timer_enabled) offload_timer_stop(pnode);
168 
169 #define OFFLOAD_TIMER_INIT() \
170     if (timer_enabled) offload_timer_init();
171 
172 #define OFFLOAD_TIMER_TARGET_DATA(data) \
173     if (timer_enabled) offload_timer_fill_target_data(data);
174 
175 extern DLL_LOCAL void offload_timer_start(OffloadTargetPhase t_node);
176 extern DLL_LOCAL void offload_timer_stop(OffloadTargetPhase t_node);
177 extern DLL_LOCAL void offload_timer_init(void);
178 extern DLL_LOCAL void offload_timer_fill_target_data(void *data);
179 
180 #endif // HOST_LIBRARY
181 
182 #else // TIMING_SUPPORT
183 
184 #define OFFLOAD_TIMER_START(...)
185 #define OFFLOAD_TIMER_STOP(...)
186 #define OFFLOAD_TIMER_INIT(...)
187 #define OFFLOAD_TIMER_TARGET_DATA(...)
188 #define OFFLOAD_TIMER_DATALEN(...)      (0)
189 
190 #endif // TIMING_SUPPORT
191 
192 #endif // OFFLOAD_TIMER_H_INCLUDED
193