├── .gitignore ├── LICENSE ├── Makefile ├── README.md └── hpm_counters.cxx /.gitignore: -------------------------------------------------------------------------------- 1 | hpm_counters 2 | hpm_counters.dump 3 | 4 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "{}" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright 2017 University of California, Berkeley 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | 2 | CC=riscv64-unknown-linux-gnu-g++ 3 | CFLAGS=-O2 4 | LDFLAGS= 5 | 6 | hpm_counters: hpm_counters.cxx Makefile 7 | $(CC) $(CFLAGS) -o $@ $@.cxx 8 | riscv-objdump --disassemble-all $@ > $@.dump 9 | 10 | # likely won't work, as we're compiled using glibc 11 | run: hpm_counters 12 | spike pk ./hpm_counters 13 | 14 | clean: 15 | rm -f hpm_counters *.o *.dump 16 | 17 | .PHONY: run clean 18 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | **About** 2 | 3 | This utility helps monitor performance of RISC-V processors running Linux. 4 | 5 | RISC-V processors implement Hardware Performance Monitor (HPM) counters. 6 | 7 | So long as these counters are unprotected and not saved and restored on context 8 | switches, Process A can read the HPM counters to monitor the performance 9 | of Process B. 10 | 11 | `hpm_counters` is a binary that can run in Linux and checkpoint the HPM counters 12 | every (roughly) 100M cycles (you can set the sleep time). When `hpm_counters` is 13 | sent a SIGKILL, it will print out all of its checkpoints. 14 | 15 | **Requirements** 16 | 17 | You must install the glibc/Linux RISC-V gcc cross compiler (`riscv64-unknown-linux-gnu-g++`). 18 | 19 | You must modify the machine initialization in your boot process to initialize 20 | the HPM counters are desired. You must also give user-access privileges to the 21 | HPM counters: 22 | 23 | ```` 24 | // For the Berkeley Boot Loader, riscv-tools/riscv-pk/machine/minit.c 25 | static void mstatus_init() 26 | { 27 | ... 28 | // Enable user/supervisor use of perf counters 29 | write_csr(mucounteren, -1); 30 | write_csr(mscounteren, -1); 31 | 32 | // Set the HPM event selectors as desired for your specific micro-architecture 33 | write_csr(mhpmevent3, 1) 34 | write_csr(mhpmevent4, 2) 35 | write_csr(mhpmevent5, 3) 36 | ... 37 | } 38 | ```` 39 | 40 | 41 | **How to Use?** 42 | 43 | ```` 44 | $ ./hpm_counters & 45 | $ ./mybenchmark 46 | $ killall hpm_counters 47 | $ while pgrep hpm_counters > /dev/null; do sleep 1; done 48 | ```` 49 | 50 | -------------------------------------------------------------------------------- /hpm_counters.cxx: -------------------------------------------------------------------------------- 1 | // Copyright 2017 University of California, Berkeley. See LICENSE. 2 | 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | 10 | // In setStats, we might trap reading uarch-specific counters. 11 | // The trap handler will skip over the instruction and write 0, 12 | // but only if a0 is the destination register. 13 | #define read_csr_safe(reg) ({ register long __tmp asm("a0"); \ 14 | asm volatile ("csrr %0, " #reg : "=r"(__tmp)); \ 15 | __tmp; }) 16 | 17 | // 100e6 cycles 18 | #define SLEEP_TIME_US (100000) 19 | 20 | // How many counters do we support? (change for your micro-architecture). 21 | #define NUM_COUNTERS (8) 22 | //#define NUM_COUNTERS (32) maximum amount of HPMs is 32 23 | typedef std::array snapshot_t; 24 | 25 | static char const* counter_names[NUM_COUNTERS]; 26 | static snapshot_t init_counters; 27 | static std::vector counters; 28 | 29 | 30 | // use sprintf to attempt to lesson load on debug interface 31 | #define CHAR_PER_LINE (40) 32 | #define MAX_BUFFER_SZ (CHAR_PER_LINE * NUM_COUNTERS) 33 | 34 | enum StatState 35 | { 36 | INIT, // initialize the counters 37 | WAKEUP, // print the running diff from last snapshot 38 | FINISH, // print the total CPI 39 | MAX 40 | }; 41 | 42 | int bytes_added(int result) 43 | { 44 | if (result > 0) { 45 | return result; 46 | } else { 47 | printf ("Error in sprintf. Res: %d\n", result); 48 | return 0; 49 | } 50 | } 51 | 52 | #if 1 53 | static int handle_stats(int enable) 54 | { 55 | long tsc_start = read_csr_safe(cycle); 56 | long irt_start = read_csr_safe(instret); 57 | 58 | sigset_t sig_set; 59 | sigemptyset(&sig_set); 60 | sigaddset(&sig_set, SIGTERM); 61 | 62 | if (sigprocmask(SIG_BLOCK, &sig_set, NULL) < 0) { 63 | perror ("sigprocmask failed"); 64 | return 1; 65 | } 66 | 67 | static size_t step = 0; // increment every time handle_stats is called 68 | 69 | int i = 0; 70 | snapshot_t snapshot; 71 | #define READ_CTR(name) do { \ 72 | if (i < NUM_COUNTERS) { \ 73 | long csr = read_csr_safe(name); \ 74 | if (enable == INIT) { init_counters[i] = csr; snapshot[i] = 0; counter_names[i] = #name; } \ 75 | if (enable == WAKEUP) { snapshot[i] = csr - init_counters[i]; } \ 76 | if (enable == FINISH) { snapshot[i] = csr - init_counters[i]; } \ 77 | i++; \ 78 | } \ 79 | } while (0) 80 | 81 | // Since most processors will not support all 32 HPMs, comment out which hpm counters you don't want to track. 82 | READ_CTR(cycle); 83 | READ_CTR(instret); 84 | READ_CTR(time); 85 | READ_CTR(hpmcounter3); 86 | READ_CTR(hpmcounter4); 87 | READ_CTR(hpmcounter5); 88 | READ_CTR(hpmcounter6); 89 | READ_CTR(hpmcounter7); 90 | READ_CTR(hpmcounter8); 91 | READ_CTR(hpmcounter9); 92 | READ_CTR(hpmcounter10); 93 | READ_CTR(hpmcounter11); 94 | READ_CTR(hpmcounter12); 95 | READ_CTR(hpmcounter13); 96 | READ_CTR(hpmcounter14); 97 | READ_CTR(hpmcounter15); 98 | READ_CTR(hpmcounter16); 99 | READ_CTR(hpmcounter17); 100 | READ_CTR(hpmcounter18); 101 | READ_CTR(hpmcounter19); 102 | READ_CTR(hpmcounter20); 103 | READ_CTR(hpmcounter21); 104 | READ_CTR(hpmcounter22); 105 | READ_CTR(hpmcounter23); 106 | READ_CTR(hpmcounter24); 107 | READ_CTR(hpmcounter25); 108 | READ_CTR(hpmcounter26); 109 | READ_CTR(hpmcounter27); 110 | READ_CTR(hpmcounter28); 111 | READ_CTR(hpmcounter29); 112 | READ_CTR(hpmcounter30); 113 | READ_CTR(hpmcounter31); 114 | 115 | counters.push_back(snapshot); 116 | 117 | //printf("Snapshot Time in cycles : %ld\n", read_csr_safe(cycle) - tsc_start); 118 | //printf("Snapshot Time in instret: %ld\n", read_csr_safe(instret) - irt_start); 119 | //if (step % 10 == 0) printf("heartbeat: %d\n", step); 120 | step++; 121 | 122 | #undef READ_CTR 123 | if (enable == FINISH || step % 30 == 0) { 124 | for (auto & element : counters) { 125 | for (int i = 0; i < NUM_COUNTERS; i++) { 126 | long c = element[i]; 127 | if (c) { 128 | printf("## %s = %ld\n", counter_names[i], c); 129 | } 130 | } 131 | } 132 | if (enable != FINISH) counters.clear(); 133 | 134 | //printf("Print Time in cycles : %ld\n", read_csr_safe(cycle) - tsc_start); 135 | //printf("Print Time in instret: %ld\n", read_csr_safe(instret) - irt_start); 136 | } 137 | 138 | if (sigprocmask(SIG_UNBLOCK, &sig_set, NULL) < 0) { 139 | perror ("sigprocmask unblock failed"); 140 | return 1; 141 | } 142 | 143 | return 0; 144 | } 145 | #else 146 | static int handle_stats(int enable) { return 0; } 147 | #endif 148 | 149 | void sig_handler(int signum) 150 | { 151 | handle_stats(FINISH); 152 | //printf("HPM Counters exiting, received handler: %d\n", signum); 153 | exit(0); 154 | } 155 | 156 | int main(int argc, char** argv) 157 | { 158 | signal(SIGINT, sig_handler); 159 | signal(SIGTERM, sig_handler); 160 | 161 | if (argc > 1) 162 | { 163 | // only print the final cycle and instret counts. 164 | //printf ("Pausing, argc=%d\n", argc); 165 | handle_stats(INIT); 166 | pause(); 167 | } 168 | else 169 | { 170 | //printf("Starting: counter array size: %d\n", sizeof(counters)); 171 | handle_stats(INIT); 172 | while (1) 173 | { 174 | usleep(SLEEP_TIME_US); 175 | handle_stats(WAKEUP); 176 | } 177 | //printf("Exiting\n"); 178 | } 179 | 180 | return 0; 181 | } 182 | --------------------------------------------------------------------------------