Shad Ansari | 2f7f9be | 2017-06-07 13:34:53 -0700 | [diff] [blame^] | 1 | /* |
| 2 | <:copyright-BRCM:2014:proprietary:standard |
| 3 | |
| 4 | Copyright (c) 2014 Broadcom Corporation |
| 5 | All Rights Reserved |
| 6 | |
| 7 | This program is the proprietary software of Broadcom Corporation and/or its |
| 8 | licensors, and may only be used, duplicated, modified or distributed pursuant |
| 9 | to the terms and conditions of a separate, written license agreement executed |
| 10 | between you and Broadcom (an "Authorized License"). Except as set forth in |
| 11 | an Authorized License, Broadcom grants no license (express or implied), right |
| 12 | to use, or waiver of any kind with respect to the Software, and Broadcom |
| 13 | expressly reserves all rights in and to the Software and all intellectual |
| 14 | property rights therein. IF YOU HAVE NO AUTHORIZED LICENSE, THEN YOU HAVE |
| 15 | NO RIGHT TO USE THIS SOFTWARE IN ANY WAY, AND SHOULD IMMEDIATELY NOTIFY |
| 16 | BROADCOM AND DISCONTINUE ALL USE OF THE SOFTWARE. |
| 17 | |
| 18 | Except as expressly set forth in the Authorized License, |
| 19 | |
| 20 | 1. This program, including its structure, sequence and organization, |
| 21 | constitutes the valuable trade secrets of Broadcom, and you shall use |
| 22 | all reasonable efforts to protect the confidentiality thereof, and to |
| 23 | use this information only in connection with your use of Broadcom |
| 24 | integrated circuit products. |
| 25 | |
| 26 | 2. TO THE MAXIMUM EXTENT PERMITTED BY LAW, THE SOFTWARE IS PROVIDED "AS IS" |
| 27 | AND WITH ALL FAULTS AND BROADCOM MAKES NO PROMISES, REPRESENTATIONS OR |
| 28 | WARRANTIES, EITHER EXPRESS, IMPLIED, STATUTORY, OR OTHERWISE, WITH |
| 29 | RESPECT TO THE SOFTWARE. BROADCOM SPECIFICALLY DISCLAIMS ANY AND |
| 30 | ALL IMPLIED WARRANTIES OF TITLE, MERCHANTABILITY, NONINFRINGEMENT, |
| 31 | FITNESS FOR A PARTICULAR PURPOSE, LACK OF VIRUSES, ACCURACY OR |
| 32 | COMPLETENESS, QUIET ENJOYMENT, QUIET POSSESSION OR CORRESPONDENCE |
| 33 | TO DESCRIPTION. YOU ASSUME THE ENTIRE RISK ARISING OUT OF USE OR |
| 34 | PERFORMANCE OF THE SOFTWARE. |
| 35 | |
| 36 | 3. TO THE MAXIMUM EXTENT PERMITTED BY LAW, IN NO EVENT SHALL BROADCOM OR |
| 37 | ITS LICENSORS BE LIABLE FOR (i) CONSEQUENTIAL, INCIDENTAL, SPECIAL, |
| 38 | INDIRECT, OR EXEMPLARY DAMAGES WHATSOEVER ARISING OUT OF OR IN ANY |
| 39 | WAY RELATING TO YOUR USE OF OR INABILITY TO USE THE SOFTWARE EVEN |
| 40 | IF BROADCOM HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES; |
| 41 | OR (ii) ANY AMOUNT IN EXCESS OF THE AMOUNT ACTUALLY PAID FOR THE |
| 42 | SOFTWARE ITSELF OR U.S. $1, WHICHEVER IS GREATER. THESE LIMITATIONS |
| 43 | SHALL APPLY NOTWITHSTANDING ANY FAILURE OF ESSENTIAL PURPOSE OF ANY |
| 44 | LIMITED REMEDY. |
| 45 | :> |
| 46 | */ |
| 47 | #ifndef _BCMOLT_TIME_MEASUREMENT_H_ |
| 48 | #define _BCMOLT_TIME_MEASUREMENT_H_ |
| 49 | |
| 50 | #include <bcmos_common.h> |
| 51 | #include <bcm_dev_log.h> |
| 52 | |
| 53 | /* How to use the time measurement library: |
| 54 | * |
| 55 | * Before including bcmolt_time_measurement.h, you must define BCMOLT_TIME_MEASUREMENT_TIMESTAMP. This macro must be |
| 56 | * defined in such a way that this is legal: "uint32_t timestamp_in_ticks = BCMOLT_TIME_MEASUREMENT_TIMESTAMP()". We |
| 57 | * use a macro instead of a function to optimize for time (we want to avoid the overhead of a function call). |
| 58 | * |
| 59 | * You can have as many named measurement categories as desired. For each category, you need to call either: |
| 60 | * - BCMOLT_TIME_MEASUREMENT_SAMPLE_DECL_STATIC() - if the measurements are all taken in the same file |
| 61 | * or: |
| 62 | * - BCMOLT_TIME_MEASUREMENT_SAMPLE_DECL() + BCMOLT_TIME_MEASUREMENT_SAMPLE_DECL_EXTERN() |
| 63 | * |
| 64 | * At init time, you must call bcmolt_time_measurement_init(). |
| 65 | * |
| 66 | * For the actual measurements, you must surround the code you're trying to measure with |
| 67 | * BCMOLT_TIME_MEASUREMENTS_SAMPLE_BEFORE() and BCMOLT_TIME_MEASUREMENTS_SAMPLE_AFTER(). At the end of a full |
| 68 | * measurement cycle, you must call BCMOLT_TIME_MEASUREMENTS_SAMPLE_END() for each named measurement category, then |
| 69 | * finally call BCMOLT_TIME_MEASUREMENTS_CYCLE_END(). |
| 70 | */ |
| 71 | |
| 72 | typedef uint64_t (*F_bcmolt_time_measurement_ticks_to_ns)(uint64_t ticks); |
| 73 | typedef void (*F_bcmolt_time_measurement_avg_done)(void); |
| 74 | |
| 75 | /** Called for each measurement category at compile time (see how-to at top of file). */ |
| 76 | #define BCMOLT_TIME_MEASUREMENT_SAMPLE_DECL_STATIC(measurement_name) \ |
| 77 | static uint32_t measurement_name ## _start_tick; \ |
| 78 | static uint64_t measurement_name ## _total_ticks; \ |
| 79 | static uint32_t measurement_name ## _num_of_samples; |
| 80 | |
| 81 | /** Called for each measurement category at compile time (see how-to at top of file). */ |
| 82 | #define BCMOLT_TIME_MEASUREMENT_SAMPLE_DECL(measurement_name) \ |
| 83 | uint32_t measurement_name ## _start_tick; \ |
| 84 | uint64_t measurement_name ## _total_ticks; \ |
| 85 | uint32_t measurement_name ## _num_of_samples; |
| 86 | |
| 87 | /** Called for each measurement category at compile time (see how-to at top of file). */ |
| 88 | #define BCMOLT_TIME_MEASUREMENT_SAMPLE_DECL_EXTERN(measurement_name) \ |
| 89 | extern uint32_t measurement_name ## _start_tick; \ |
| 90 | extern uint64_t measurement_name ## _total_ticks; \ |
| 91 | extern uint32_t measurement_name ## _num_of_samples; |
| 92 | |
| 93 | /** Called before each block of code that needs to be timed (see how-to at top of file). */ |
| 94 | #define BCMOLT_TIME_MEASUREMENTS_SAMPLE_BEFORE(measurement_name) \ |
| 95 | do \ |
| 96 | { \ |
| 97 | measurement_name ## _start_tick = BCMOLT_TIME_MEASUREMENT_TIMESTAMP(); \ |
| 98 | } \ |
| 99 | while (0) |
| 100 | |
| 101 | /** Called after each block of code that needs to be timed (see how-to at top of file). */ |
| 102 | #define BCMOLT_TIME_MEASUREMENTS_SAMPLE_AFTER(measurement_name) \ |
| 103 | do \ |
| 104 | { \ |
| 105 | /* Even if timestamp overflows we are still ok - unsigned arithmetic will still be valid. */ \ |
| 106 | measurement_name ## _total_ticks += BCMOLT_TIME_MEASUREMENT_TIMESTAMP() - measurement_name ## _start_tick; \ |
| 107 | ++measurement_name ## _num_of_samples; \ |
| 108 | } \ |
| 109 | while (0) |
| 110 | |
| 111 | /** Called for each measurement category at the end of a cycle (see how-to at top of file). */ |
| 112 | #define BCMOLT_TIME_MEASUREMENTS_SAMPLE_END(measurement_name) \ |
| 113 | do \ |
| 114 | { \ |
| 115 | bcmolt_time_measurement_sample_end( \ |
| 116 | &measurement_name ## _total_ticks, \ |
| 117 | &measurement_name ## _num_of_samples, \ |
| 118 | # measurement_name); \ |
| 119 | } \ |
| 120 | while (0) |
| 121 | |
| 122 | /** Called when the entire measurement cycle is complete (see how-to at top of file). */ |
| 123 | #define BCMOLT_TIME_MEASUREMENTS_CYCLE_END() bcmolt_time_measurement_cycle_end() |
| 124 | |
| 125 | /** Must be called at init time to initialize the time measurement library. |
| 126 | * \param log_id Log ID to use for displaying data (will use 'DEBUG' log level). |
| 127 | * \param num_cycles_for_average Number of measurement cycles to run before calculating and logging averages. |
| 128 | * \param ticks_to_ns Function that translates timestamp resolution (ticks) into nanoseconds. |
| 129 | * \param avg_done Function to call after averages have been computed (e.g. to log a custom header). |
| 130 | */ |
| 131 | void bcmolt_time_measurement_init( |
| 132 | dev_log_id log_id, |
| 133 | uint32_t num_cycles_for_average, |
| 134 | F_bcmolt_time_measurement_ticks_to_ns ticks_to_ns, |
| 135 | F_bcmolt_time_measurement_avg_done avg_done); |
| 136 | |
| 137 | /** Do not call this directly - call BCMOLT_TIME_MEASUREMENTS_SAMPLE_END() instead. */ |
| 138 | void bcmolt_time_measurement_sample_end(uint64_t *total_ticks, uint32_t *num_of_samples, const char *measurement_name); |
| 139 | |
| 140 | /** Do not call this directly - call BCMOLT_TIME_MEASUREMENTS_CYCLE_END() instead. */ |
| 141 | void bcmolt_time_measurement_cycle_end(void); |
| 142 | |
| 143 | #endif /* _BCMOLT_TIME_MEASUREMENT_H_ */ |