├── LICENSE ├── Makefile ├── README ├── buf ├── Makefile ├── athlonX2.ods ├── buf.c ├── core2quad.ods ├── corei7.ods └── geomean.ods ├── getpid ├── Makefile └── getpid.c ├── matrix ├── Makefile ├── matrix.c └── matrix.h ├── moreExamples ├── Makefile └── moreExamples.c ├── rdtscbench ├── Makefile ├── gethz.sh ├── rdtscbench.c └── rdtscbench.h └── stacko ├── Makefile.noptimize ├── Makefile.optimize ├── README ├── graphs.ods └── stacko.c /LICENSE: -------------------------------------------------------------------------------- 1 | GNU GENERAL PUBLIC LICENSE 2 | Version 2, June 1991 3 | 4 | Copyright (C) 1989, 1991 Free Software Foundation, Inc., 5 | 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 6 | Everyone is permitted to copy and distribute verbatim copies 7 | of this license document, but changing it is not allowed. 8 | 9 | Preamble 10 | 11 | The licenses for most software are designed to take away your 12 | freedom to share and change it. By contrast, the GNU General Public 13 | License is intended to guarantee your freedom to share and change free 14 | software--to make sure the software is free for all its users. This 15 | General Public License applies to most of the Free Software 16 | Foundation's software and to any other program whose authors commit to 17 | using it. (Some other Free Software Foundation software is covered by 18 | the GNU Lesser General Public License instead.) You can apply it to 19 | your programs, too. 20 | 21 | When we speak of free software, we are referring to freedom, not 22 | price. Our General Public Licenses are designed to make sure that you 23 | have the freedom to distribute copies of free software (and charge for 24 | this service if you wish), that you receive source code or can get it 25 | if you want it, that you can change the software or use pieces of it 26 | in new free programs; and that you know you can do these things. 27 | 28 | To protect your rights, we need to make restrictions that forbid 29 | anyone to deny you these rights or to ask you to surrender the rights. 30 | These restrictions translate to certain responsibilities for you if you 31 | distribute copies of the software, or if you modify it. 32 | 33 | For example, if you distribute copies of such a program, whether 34 | gratis or for a fee, you must give the recipients all the rights that 35 | you have. You must make sure that they, too, receive or can get the 36 | source code. And you must show them these terms so they know their 37 | rights. 38 | 39 | We protect your rights with two steps: (1) copyright the software, and 40 | (2) offer you this license which gives you legal permission to copy, 41 | distribute and/or modify the software. 42 | 43 | Also, for each author's protection and ours, we want to make certain 44 | that everyone understands that there is no warranty for this free 45 | software. If the software is modified by someone else and passed on, we 46 | want its recipients to know that what they have is not the original, so 47 | that any problems introduced by others will not reflect on the original 48 | authors' reputations. 49 | 50 | Finally, any free program is threatened constantly by software 51 | patents. We wish to avoid the danger that redistributors of a free 52 | program will individually obtain patent licenses, in effect making the 53 | program proprietary. To prevent this, we have made it clear that any 54 | patent must be licensed for everyone's free use or not licensed at all. 55 | 56 | The precise terms and conditions for copying, distribution and 57 | modification follow. 58 | 59 | GNU GENERAL PUBLIC LICENSE 60 | TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION 61 | 62 | 0. This License applies to any program or other work which contains 63 | a notice placed by the copyright holder saying it may be distributed 64 | under the terms of this General Public License. The "Program", below, 65 | refers to any such program or work, and a "work based on the Program" 66 | means either the Program or any derivative work under copyright law: 67 | that is to say, a work containing the Program or a portion of it, 68 | either verbatim or with modifications and/or translated into another 69 | language. (Hereinafter, translation is included without limitation in 70 | the term "modification".) Each licensee is addressed as "you". 71 | 72 | Activities other than copying, distribution and modification are not 73 | covered by this License; they are outside its scope. The act of 74 | running the Program is not restricted, and the output from the Program 75 | is covered only if its contents constitute a work based on the 76 | Program (independent of having been made by running the Program). 77 | Whether that is true depends on what the Program does. 78 | 79 | 1. You may copy and distribute verbatim copies of the Program's 80 | source code as you receive it, in any medium, provided that you 81 | conspicuously and appropriately publish on each copy an appropriate 82 | copyright notice and disclaimer of warranty; keep intact all the 83 | notices that refer to this License and to the absence of any warranty; 84 | and give any other recipients of the Program a copy of this License 85 | along with the Program. 86 | 87 | You may charge a fee for the physical act of transferring a copy, and 88 | you may at your option offer warranty protection in exchange for a fee. 89 | 90 | 2. You may modify your copy or copies of the Program or any portion 91 | of it, thus forming a work based on the Program, and copy and 92 | distribute such modifications or work under the terms of Section 1 93 | above, provided that you also meet all of these conditions: 94 | 95 | a) You must cause the modified files to carry prominent notices 96 | stating that you changed the files and the date of any change. 97 | 98 | b) You must cause any work that you distribute or publish, that in 99 | whole or in part contains or is derived from the Program or any 100 | part thereof, to be licensed as a whole at no charge to all third 101 | parties under the terms of this License. 102 | 103 | c) If the modified program normally reads commands interactively 104 | when run, you must cause it, when started running for such 105 | interactive use in the most ordinary way, to print or display an 106 | announcement including an appropriate copyright notice and a 107 | notice that there is no warranty (or else, saying that you provide 108 | a warranty) and that users may redistribute the program under 109 | these conditions, and telling the user how to view a copy of this 110 | License. (Exception: if the Program itself is interactive but 111 | does not normally print such an announcement, your work based on 112 | the Program is not required to print an announcement.) 113 | 114 | These requirements apply to the modified work as a whole. If 115 | identifiable sections of that work are not derived from the Program, 116 | and can be reasonably considered independent and separate works in 117 | themselves, then this License, and its terms, do not apply to those 118 | sections when you distribute them as separate works. But when you 119 | distribute the same sections as part of a whole which is a work based 120 | on the Program, the distribution of the whole must be on the terms of 121 | this License, whose permissions for other licensees extend to the 122 | entire whole, and thus to each and every part regardless of who wrote it. 123 | 124 | Thus, it is not the intent of this section to claim rights or contest 125 | your rights to work written entirely by you; rather, the intent is to 126 | exercise the right to control the distribution of derivative or 127 | collective works based on the Program. 128 | 129 | In addition, mere aggregation of another work not based on the Program 130 | with the Program (or with a work based on the Program) on a volume of 131 | a storage or distribution medium does not bring the other work under 132 | the scope of this License. 133 | 134 | 3. You may copy and distribute the Program (or a work based on it, 135 | under Section 2) in object code or executable form under the terms of 136 | Sections 1 and 2 above provided that you also do one of the following: 137 | 138 | a) Accompany it with the complete corresponding machine-readable 139 | source code, which must be distributed under the terms of Sections 140 | 1 and 2 above on a medium customarily used for software interchange; or, 141 | 142 | b) Accompany it with a written offer, valid for at least three 143 | years, to give any third party, for a charge no more than your 144 | cost of physically performing source distribution, a complete 145 | machine-readable copy of the corresponding source code, to be 146 | distributed under the terms of Sections 1 and 2 above on a medium 147 | customarily used for software interchange; or, 148 | 149 | c) Accompany it with the information you received as to the offer 150 | to distribute corresponding source code. (This alternative is 151 | allowed only for noncommercial distribution and only if you 152 | received the program in object code or executable form with such 153 | an offer, in accord with Subsection b above.) 154 | 155 | The source code for a work means the preferred form of the work for 156 | making modifications to it. For an executable work, complete source 157 | code means all the source code for all modules it contains, plus any 158 | associated interface definition files, plus the scripts used to 159 | control compilation and installation of the executable. However, as a 160 | special exception, the source code distributed need not include 161 | anything that is normally distributed (in either source or binary 162 | form) with the major components (compiler, kernel, and so on) of the 163 | operating system on which the executable runs, unless that component 164 | itself accompanies the executable. 165 | 166 | If distribution of executable or object code is made by offering 167 | access to copy from a designated place, then offering equivalent 168 | access to copy the source code from the same place counts as 169 | distribution of the source code, even though third parties are not 170 | compelled to copy the source along with the object code. 171 | 172 | 4. You may not copy, modify, sublicense, or distribute the Program 173 | except as expressly provided under this License. Any attempt 174 | otherwise to copy, modify, sublicense or distribute the Program is 175 | void, and will automatically terminate your rights under this License. 176 | However, parties who have received copies, or rights, from you under 177 | this License will not have their licenses terminated so long as such 178 | parties remain in full compliance. 179 | 180 | 5. You are not required to accept this License, since you have not 181 | signed it. However, nothing else grants you permission to modify or 182 | distribute the Program or its derivative works. These actions are 183 | prohibited by law if you do not accept this License. Therefore, by 184 | modifying or distributing the Program (or any work based on the 185 | Program), you indicate your acceptance of this License to do so, and 186 | all its terms and conditions for copying, distributing or modifying 187 | the Program or works based on it. 188 | 189 | 6. Each time you redistribute the Program (or any work based on the 190 | Program), the recipient automatically receives a license from the 191 | original licensor to copy, distribute or modify the Program subject to 192 | these terms and conditions. You may not impose any further 193 | restrictions on the recipients' exercise of the rights granted herein. 194 | You are not responsible for enforcing compliance by third parties to 195 | this License. 196 | 197 | 7. If, as a consequence of a court judgment or allegation of patent 198 | infringement or for any other reason (not limited to patent issues), 199 | conditions are imposed on you (whether by court order, agreement or 200 | otherwise) that contradict the conditions of this License, they do not 201 | excuse you from the conditions of this License. If you cannot 202 | distribute so as to satisfy simultaneously your obligations under this 203 | License and any other pertinent obligations, then as a consequence you 204 | may not distribute the Program at all. For example, if a patent 205 | license would not permit royalty-free redistribution of the Program by 206 | all those who receive copies directly or indirectly through you, then 207 | the only way you could satisfy both it and this License would be to 208 | refrain entirely from distribution of the Program. 209 | 210 | If any portion of this section is held invalid or unenforceable under 211 | any particular circumstance, the balance of the section is intended to 212 | apply and the section as a whole is intended to apply in other 213 | circumstances. 214 | 215 | It is not the purpose of this section to induce you to infringe any 216 | patents or other property right claims or to contest validity of any 217 | such claims; this section has the sole purpose of protecting the 218 | integrity of the free software distribution system, which is 219 | implemented by public license practices. Many people have made 220 | generous contributions to the wide range of software distributed 221 | through that system in reliance on consistent application of that 222 | system; it is up to the author/donor to decide if he or she is willing 223 | to distribute software through any other system and a licensee cannot 224 | impose that choice. 225 | 226 | This section is intended to make thoroughly clear what is believed to 227 | be a consequence of the rest of this License. 228 | 229 | 8. If the distribution and/or use of the Program is restricted in 230 | certain countries either by patents or by copyrighted interfaces, the 231 | original copyright holder who places the Program under this License 232 | may add an explicit geographical distribution limitation excluding 233 | those countries, so that distribution is permitted only in or among 234 | countries not thus excluded. In such case, this License incorporates 235 | the limitation as if written in the body of this License. 236 | 237 | 9. The Free Software Foundation may publish revised and/or new versions 238 | of the General Public License from time to time. Such new versions will 239 | be similar in spirit to the present version, but may differ in detail to 240 | address new problems or concerns. 241 | 242 | Each version is given a distinguishing version number. If the Program 243 | specifies a version number of this License which applies to it and "any 244 | later version", you have the option of following the terms and conditions 245 | either of that version or of any later version published by the Free 246 | Software Foundation. If the Program does not specify a version number of 247 | this License, you may choose any version ever published by the Free Software 248 | Foundation. 249 | 250 | 10. If you wish to incorporate parts of the Program into other free 251 | programs whose distribution conditions are different, write to the author 252 | to ask for permission. For software which is copyrighted by the Free 253 | Software Foundation, write to the Free Software Foundation; we sometimes 254 | make exceptions for this. Our decision will be guided by the two goals 255 | of preserving the free status of all derivatives of our free software and 256 | of promoting the sharing and reuse of software generally. 257 | 258 | NO WARRANTY 259 | 260 | 11. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY 261 | FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN 262 | OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES 263 | PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED 264 | OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 265 | MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS 266 | TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE 267 | PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, 268 | REPAIR OR CORRECTION. 269 | 270 | 12. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING 271 | WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR 272 | REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, 273 | INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING 274 | OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED 275 | TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY 276 | YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER 277 | PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE 278 | POSSIBILITY OF SUCH DAMAGES. 279 | 280 | END OF TERMS AND CONDITIONS 281 | 282 | How to Apply These Terms to Your New Programs 283 | 284 | If you develop a new program, and you want it to be of the greatest 285 | possible use to the public, the best way to achieve this is to make it 286 | free software which everyone can redistribute and change under these terms. 287 | 288 | To do so, attach the following notices to the program. It is safest 289 | to attach them to the start of each source file to most effectively 290 | convey the exclusion of warranty; and each file should have at least 291 | the "copyright" line and a pointer to where the full notice is found. 292 | 293 | 294 | Copyright (C) 295 | 296 | This program is free software; you can redistribute it and/or modify 297 | it under the terms of the GNU General Public License as published by 298 | the Free Software Foundation; either version 2 of the License, or 299 | (at your option) any later version. 300 | 301 | This program is distributed in the hope that it will be useful, 302 | but WITHOUT ANY WARRANTY; without even the implied warranty of 303 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 304 | GNU General Public License for more details. 305 | 306 | You should have received a copy of the GNU General Public License along 307 | with this program; if not, write to the Free Software Foundation, Inc., 308 | 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. 309 | 310 | Also add information on how to contact you by electronic and paper mail. 311 | 312 | If the program is interactive, make it output a short notice like this 313 | when it starts in an interactive mode: 314 | 315 | Gnomovision version 69, Copyright (C) year name of author 316 | Gnomovision comes with ABSOLUTELY NO WARRANTY; for details type `show w'. 317 | This is free software, and you are welcome to redistribute it 318 | under certain conditions; type `show c' for details. 319 | 320 | The hypothetical commands `show w' and `show c' should show the appropriate 321 | parts of the General Public License. Of course, the commands you use may 322 | be called something other than `show w' and `show c'; they could even be 323 | mouse-clicks or menu items--whatever suits your program. 324 | 325 | You should also get your employer (if you work as a programmer) or your 326 | school, if any, to sign a "copyright disclaimer" for the program, if 327 | necessary. Here is a sample; alter the names: 328 | 329 | Yoyodyne, Inc., hereby disclaims all copyright interest in the program 330 | `Gnomovision' (which makes passes at compilers) written by James Hacker. 331 | 332 | , 1 April 1989 333 | Ty Coon, President of Vice 334 | 335 | This General Public License does not permit incorporating your program into 336 | proprietary programs. If your program is a subroutine library, you may 337 | consider it more useful to permit linking proprietary applications with the 338 | library. If this is what you want to do, use the GNU Lesser General 339 | Public License instead of this License. 340 | -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | $(MAKE) -C ./rdtscbench 3 | $(MAKE) -C ./getpid 4 | $(MAKE) -C ./matrix 5 | $(MAKE) -C ./moreExamples 6 | 7 | clean: 8 | $(MAKE) -C ./rdtscbench clean 9 | $(MAKE) -C ./getpid clean 10 | $(MAKE) -C ./matrix clean 11 | $(MAKE) -C ./moreExamples clean 12 | rm -f *~ 13 | rm -f *.o 14 | 15 | -------------------------------------------------------------------------------- /README: -------------------------------------------------------------------------------- 1 | rdtscbench measures small and large operations using clock cycles as 2 | time measurement unit. It uses RDTSC x86 instruction to get clock 3 | cycles from CPU registers 4 | 5 | rdtscbench is free software licensed under the GPL v2. 6 | 7 | Peter Senna Tschudin - peter.senna@gmail.com 8 | 9 | IMPORTANT: Before running make and rdtscbench disable CPU dynamic clock. 10 | On Linux: # service cpuspeed stop 11 | 12 | IMPORTANT 2: If the computer hybernates during the measurement, it may 13 | break the measurement. 14 | 15 | The Makefile runs gethz.sh to cat /proc/cpuinfo looking for the CPU speed. 16 | The result is saved in hzdef.h. See example: 17 | $ cat /proc/cpuinfo |grep MHz 18 | cpu MHz : 2701.000 19 | gethz.sh converts 2701.000 to 2701000000LL and saves it at hzdef.h 20 | 21 | folders: 22 | 23 | buf - C code optimization tests 24 | 25 | getpid - Very simple example of how to use rdtscbench 26 | 27 | matrix - Measuring C code optimization - http://goo.gl/Pcilt 28 | 29 | moreExamples - Measuring: gettimeofday(), simplemath[], sysconf(), 30 | malloc ( 1000000 * sizeof ( char ) ), 31 | malloc ( 1000000000 * sizeof ( char ) ), 32 | Process Communication over pipes; 33 | 34 | rdtscbench - The source code of rdtscbench 35 | 36 | stacko - C code optimization tests 37 | -------------------------------------------------------------------------------- /buf/Makefile: -------------------------------------------------------------------------------- 1 | NAME = buf 2 | SRCDIR = ../rdtscbench 3 | CFLAGS = -lrt -lm 4 | OBJ = $(SRCDIR)/rdtscbench.o $(NAME).o 5 | 6 | 7 | all: 8 | $(MAKE) -C $(SRCDIR) 9 | $(MAKE) -C . $(NAME) 10 | 11 | $(NAME): $(OBJ) 12 | $(CC) $(CFLAGS) -o $(NAME) $(OBJ) 13 | 14 | $(SRCDIR)/rdtscbench.o: $(SRCDIR)/rdtscbench.h $(SRCDIR)/rdtscbench.c 15 | 16 | clean: 17 | rm -f *~ 18 | rm -f *.o 19 | rm -f hzdef.h 20 | rm -f $(NAME) 21 | # $(MAKE) -C $(SRCDIR) clean 22 | 23 | -------------------------------------------------------------------------------- /buf/athlonX2.ods: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/petersenna/rdtscbench/d5b99e7d70045361b870e8634eef20a48c68bb77/buf/athlonX2.ods -------------------------------------------------------------------------------- /buf/buf.c: -------------------------------------------------------------------------------- 1 | /* rdtscbench: measures operations in clock cycles using RDTSC x86 instruction 2 | * 3 | * Peter Senna Tschudin - peter.senna@gmail.com 4 | * 5 | * IMPORTANT: Before running make and rdtscbench disable CPU dynamic clock. 6 | * On Linux: # service cpuspeed stop 7 | * 8 | * IMPORTANT 2: If the computer hybernates during the measurement, it may 9 | * break the measurement. 10 | * 11 | * The Makefile runs gethz.sh to cat /proc/cpuinfo looking for the CPU speed. 12 | * The result is saved in hzdef.h. See example: 13 | * $ cat /proc/cpuinfo |grep MHz 14 | * cpu MHz : 2701.000 15 | * gethz.sh converts 2701.000 to 2701000000LL and saves it at hzdef.h 16 | * 17 | */ 18 | 19 | #include "../rdtscbench/rdtscbench.h" 20 | 21 | #define VIDEO_MAX_FRAME 32 22 | 23 | enum vb2_buffer_state { 24 | VB2_BUF_STATE_DEQUEUED, 25 | VB2_BUF_STATE_QUEUED, 26 | VB2_BUF_STATE_ACTIVE, 27 | VB2_BUF_STATE_DONE, 28 | VB2_BUF_STATE_ERROR, 29 | }; 30 | 31 | struct vb2_buffer { 32 | enum vb2_buffer_state state; 33 | }; 34 | 35 | struct vb2_queue { 36 | struct vb2_buffer *bufs[VIDEO_MAX_FRAME]; 37 | unsigned int num_buffers; 38 | }; 39 | 40 | void inline BUF1 ( struct timespec *ts, struct timespec *te ); 41 | void inline BUF2 ( struct timespec *ts, struct timespec *te ); 42 | 43 | int main (int argc, char *argv[]){ 44 | struct timespec begin, end; 45 | int accrepeats, repeats, detailed = 0; 46 | 47 | if ( argc < 3 ){ 48 | printf ("Error. You need to specify two numbers as arguments.\n\n %s \n\n" , argv[0]); 49 | exit (1); 50 | } 51 | repeats = atoi(argv[1]); 52 | accrepeats = atoi(argv[2]); 53 | 54 | if ( argc > 3 ) 55 | detailed = 1; 56 | 57 | rdtscb_measure ( "buf1[] ", repeats, detailed, &BUF1 ); 58 | 59 | sleep(5); /* optional */ 60 | 61 | rdtscb_measure ( "buf2[] ", repeats, detailed, &BUF2 ); 62 | 63 | sleep(5); /* optional */ 64 | 65 | 66 | /* How to use accuracy() function */ 67 | rdtscb_accuracy (accrepeats, detailed); 68 | } 69 | 70 | void inline BUF1 ( struct timespec *ts, struct timespec *te ){ 71 | 72 | unsigned int i; 73 | 74 | struct vb2_queue *q; 75 | 76 | q = malloc(sizeof(struct vb2_queue)); 77 | 78 | q->num_buffers = VIDEO_MAX_FRAME; 79 | 80 | for (i = 0; i < VIDEO_MAX_FRAME; ++i) 81 | q->bufs[i] = (struct vb2_buffer *) malloc(sizeof(struct vb2_buffer)); 82 | 83 | rdtscb_getticks ( ts ); /* start measurement */ 84 | 85 | for (i = 0; i < q->num_buffers; ++i) 86 | q->bufs[i]->state = VB2_BUF_STATE_DEQUEUED; 87 | 88 | rdtscb_getticks ( te ); /* end measurement */ 89 | } 90 | 91 | void inline BUF2 ( struct timespec *ts, struct timespec *te ){ 92 | 93 | unsigned int i; 94 | 95 | struct vb2_queue *q; 96 | 97 | struct vb2_buffer *buf_ptr, *buf_ptr_end; 98 | 99 | q = malloc(sizeof(struct vb2_queue)); 100 | 101 | q->num_buffers = VIDEO_MAX_FRAME; 102 | 103 | for (i = 0; i < VIDEO_MAX_FRAME; ++i) 104 | q->bufs[i] = (struct vb2_buffer *) malloc(sizeof(struct vb2_buffer)); 105 | 106 | rdtscb_getticks ( ts ); /* start measurement */ 107 | 108 | buf_ptr_end = q->bufs[q->num_buffers]; 109 | 110 | for (buf_ptr = q->bufs[0]; buf_ptr < buf_ptr_end; ++buf_ptr){ 111 | buf_ptr->state = VB2_BUF_STATE_DEQUEUED; 112 | } 113 | 114 | rdtscb_getticks ( te ); /* end measurement */ 115 | } 116 | 117 | -------------------------------------------------------------------------------- /buf/core2quad.ods: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/petersenna/rdtscbench/d5b99e7d70045361b870e8634eef20a48c68bb77/buf/core2quad.ods -------------------------------------------------------------------------------- /buf/corei7.ods: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/petersenna/rdtscbench/d5b99e7d70045361b870e8634eef20a48c68bb77/buf/corei7.ods -------------------------------------------------------------------------------- /buf/geomean.ods: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/petersenna/rdtscbench/d5b99e7d70045361b870e8634eef20a48c68bb77/buf/geomean.ods -------------------------------------------------------------------------------- /getpid/Makefile: -------------------------------------------------------------------------------- 1 | NAME = getpid 2 | SRCDIR = ../rdtscbench 3 | CFLAGS = -lrt -lm 4 | OBJ = $(SRCDIR)/rdtscbench.o $(NAME).o 5 | 6 | 7 | all: 8 | $(MAKE) -C $(SRCDIR) 9 | $(MAKE) -C . $(NAME) 10 | 11 | $(NAME): $(OBJ) 12 | $(CC) $(CFLAGS) -o $(NAME) $(OBJ) 13 | 14 | $(SRCDIR)/rdtscbench.o: $(SRCDIR)/rdtscbench.h $(SRCDIR)/rdtscbench.c 15 | 16 | clean: 17 | rm -f *~ 18 | rm -f *.o 19 | rm -f hzdef.h 20 | rm -f $(NAME) 21 | # $(MAKE) -C $(SRCDIR) clean 22 | 23 | -------------------------------------------------------------------------------- /getpid/getpid.c: -------------------------------------------------------------------------------- 1 | /* rdtscbench: measures operations in clock cycles using RDTSC x86 instruction 2 | * 3 | * Peter Senna Tschudin - peter.senna@gmail.com 4 | * 5 | * IMPORTANT: Before running make and rdtscbench disable CPU dynamic clock. 6 | * On Linux: # service cpuspeed stop 7 | * 8 | * IMPORTANT 2: If the computer hybernates during the measurement, it may 9 | * break the measurement. 10 | * 11 | * The Makefile runs gethz.sh to cat /proc/cpuinfo looking for the CPU speed. 12 | * The result is saved in hzdef.h. See example: 13 | * $ cat /proc/cpuinfo |grep MHz 14 | * cpu MHz : 2701.000 15 | * gethz.sh converts 2701.000 to 2701000000LL and saves it at hzdef.h 16 | * 17 | */ 18 | 19 | #include "../rdtscbench/rdtscbench.h" 20 | 21 | /* 22 | * Sample function that shows how to use rdtscbench to measure operations 23 | */ 24 | void GETPID ( struct timespec *ts, struct timespec *te ); 25 | 26 | int main (int argc, char *argv[]){ 27 | struct timespec begin, end; 28 | int accrepeats, repeats, detailed = 0; 29 | 30 | if ( argc < 3 ){ 31 | printf ("Error. You need to specify two numbers as arguments.\n\n %s \n\n" , argv[0]); 32 | exit (1); 33 | } 34 | repeats = atoi(argv[1]); 35 | accrepeats = atoi(argv[2]); 36 | 37 | if ( argc > 3 ) 38 | detailed = 1; 39 | 40 | /* How to use measure() function */ 41 | rdtscb_measure ( "getpid() ", repeats, detailed, &GETPID ); 42 | 43 | sleep(5); /* optional */ 44 | 45 | /* How to use accuracy() function */ 46 | rdtscb_accuracy (accrepeats, detailed); 47 | } 48 | 49 | /* 50 | * Sample function 51 | * 52 | * add "getticks ( ts );" before what you want to measure. "s" from ts is start 53 | * 54 | * add "getticks ( te );" after what you want to measure. "e" from te is end 55 | */ 56 | void inline GETPID ( struct timespec *ts, struct timespec *te ){ 57 | 58 | pid_t pid; /* time to do this is intentionally not measured */ 59 | 60 | rdtscb_getticks ( ts ); /* start measurement */ 61 | 62 | pid = getpid(); /* action to be measured */ 63 | 64 | rdtscb_getticks ( te ); /* end measurement */ 65 | 66 | } 67 | -------------------------------------------------------------------------------- /matrix/Makefile: -------------------------------------------------------------------------------- 1 | NAME = matrix 2 | SRCDIR = ../rdtscbench 3 | CFLAGS = -lrt -lm 4 | OBJ = $(SRCDIR)/rdtscbench.o $(NAME).o 5 | 6 | 7 | all: 8 | $(MAKE) -C $(SRCDIR) 9 | $(MAKE) -C . $(NAME) 10 | 11 | $(NAME): $(OBJ) 12 | $(CC) $(CFLAGS) -o $(NAME) $(OBJ) 13 | 14 | $(SRCDIR)/rdtscbench.o: $(SRCDIR)/rdtscbench.h $(SRCDIR)/rdtscbench.c 15 | 16 | clean: 17 | rm -f *~ 18 | rm -f *.o 19 | rm -f hzdef.h 20 | rm -f $(NAME) 21 | # $(MAKE) -C $(SRCDIR) clean 22 | 23 | -------------------------------------------------------------------------------- /matrix/matrix.c: -------------------------------------------------------------------------------- 1 | /* rdtscbench: measures operations in clock cycles using RDTSC x86 instruction 2 | * 3 | * Peter Senna Tschudin - peter.senna@gmail.com 4 | * 5 | * IMPORTANT: Before running make and rdtscbench disable CPU dynamic clock. 6 | * On Linux: # service cpuspeed stop 7 | * 8 | * IMPORTANT 2: If the computer hybernates during the measurement, it may 9 | * break the measurement. 10 | * 11 | * The Makefile runs gethz.sh to cat /proc/cpuinfo looking for the CPU speed. 12 | * The result is saved in hzdef.h. See example: 13 | * $ cat /proc/cpuinfo |grep MHz 14 | * cpu MHz : 2701.000 15 | * gethz.sh converts 2701.000 to 2701000000LL and saves it at hzdef.h 16 | * 17 | */ 18 | 19 | #include "matrix.h" 20 | 21 | #define X_SIZE 60 22 | #define Y_SIZE 30 23 | #define Y_ALIGNED 32 24 | int matrix[X_SIZE][Y_SIZE]; 25 | int matrix_a[X_SIZE][Y_ALIGNED]; 26 | 27 | int main (int argc, char *argv[]){ 28 | struct timespec begin, end; 29 | int accrepeats, repeats, detailed = 0; 30 | 31 | if ( argc < 3 ){ 32 | printf ("Error. You need to specify two numbers as arguments.\n\n %s \n\n" , argv[0]); 33 | exit (1); 34 | } 35 | repeats = atoi(argv[1]); 36 | accrepeats = atoi(argv[2]); 37 | 38 | if ( argc > 3 ) 39 | detailed = 1; 40 | 41 | rdtscb_measure ( "matrix1() ", repeats, detailed, &MATRIX1 ); 42 | sleep(5); 43 | 44 | rdtscb_measure ( "matrix2() ", repeats, detailed, &MATRIX2 ); 45 | sleep(5); 46 | 47 | rdtscb_measure ( "matrix3() ", repeats, detailed, &MATRIX3 ); 48 | sleep(5); 49 | 50 | rdtscb_measure ( "matrix4() ", repeats, detailed, &MATRIX4 ); 51 | sleep(5); 52 | 53 | rdtscb_measure ( "matrix5() ", repeats, detailed, &MATRIX5 ); 54 | sleep(5); 55 | 56 | rdtscb_measure ( "matrix6() ", repeats, detailed, &MATRIX6 ); 57 | sleep(5); 58 | 59 | rdtscb_measure ( "matrix7() ", repeats, detailed, &MATRIX7 ); 60 | sleep(5); 61 | 62 | rdtscb_measure ( "matrix8() ", repeats, detailed, &MATRIX8 ); 63 | sleep(5); 64 | 65 | rdtscb_accuracy (accrepeats, detailed ); 66 | 67 | } 68 | 69 | /* 70 | * MATRIX 1 71 | */ 72 | 73 | void inline MATRIX1 (struct timespec *ts, struct timespec *te){ 74 | int x, y; 75 | 76 | /* ### starts measuring ### */ 77 | rdtscb_getticks ( ts ); 78 | 79 | for (x = 0; x < X_SIZE; ++x){ 80 | for (y = 0; y < Y_SIZE; ++y){ 81 | matrix[x][y] = -1; 82 | } 83 | } 84 | 85 | /* ### ends measuring ### */ 86 | rdtscb_getticks ( te ); 87 | 88 | } 89 | 90 | /* 91 | * MATRIX 2 92 | */ 93 | void inline MATRIX2 (struct timespec *ts, struct timespec *te){ 94 | register int x,y; 95 | 96 | /* ### starts measuring ### */ 97 | rdtscb_getticks ( ts ); 98 | 99 | for (x = 0; x < X_SIZE; ++x){ 100 | for (y = 0; y < Y_SIZE; ++y){ 101 | matrix[x][y] = -1; 102 | } 103 | } 104 | 105 | /* ### ends measuring ### */ 106 | rdtscb_getticks ( te ); 107 | 108 | } 109 | 110 | /* 111 | * MATRIX 3 112 | */ 113 | void inline MATRIX3 (struct timespec *ts, struct timespec *te){ 114 | register int x,y; 115 | 116 | /* ### starts measuring ### */ 117 | rdtscb_getticks ( ts ); 118 | 119 | for (y = 0; y < Y_SIZE; ++y){ 120 | for (x = 0; x < X_SIZE; ++x){ 121 | matrix[x][y] = -1; 122 | } 123 | } 124 | 125 | /* ### ends measuring ### */ 126 | rdtscb_getticks ( te ); 127 | 128 | } 129 | 130 | /* 131 | * MATRIX 4 132 | */ 133 | void inline MATRIX4 (struct timespec *ts, struct timespec *te){ 134 | register int x,y; 135 | 136 | /* ### starts measuring ### */ 137 | rdtscb_getticks ( ts ); 138 | 139 | for (y = 0; y < Y_ALIGNED; ++y){ 140 | for (x = 0; x < X_SIZE; ++x){ 141 | matrix_a[x][y] = -1; 142 | } 143 | } 144 | 145 | /* ### ends measuring ### */ 146 | rdtscb_getticks ( te ); 147 | 148 | } 149 | 150 | /* 151 | * MATRIX 5 152 | */ 153 | void inline MATRIX5 (struct timespec *ts, struct timespec *te){ 154 | register int index; 155 | register int *matrix_ptr; 156 | 157 | /* ### starts measuring ### */ 158 | rdtscb_getticks ( ts ); 159 | 160 | matrix_ptr = &matrix[0][0]; 161 | 162 | for (index = 0; index < X_SIZE * Y_SIZE; ++index){ 163 | *matrix_ptr = -1; 164 | matrix_ptr++; 165 | } 166 | 167 | /* ### ends measuring ### */ 168 | rdtscb_getticks ( te ); 169 | 170 | } 171 | 172 | /* 173 | * MATRIX 6 174 | */ 175 | void inline MATRIX6 (struct timespec *ts, struct timespec *te){ 176 | register int *matrix_ptr; 177 | 178 | /* ### starts measuring ### */ 179 | rdtscb_getticks ( ts ); 180 | 181 | for (matrix_ptr = &matrix[0][0]; 182 | matrix_ptr <= &matrix[X_SIZE - 1][Y_SIZE - 1]; 183 | ++matrix_ptr){ 184 | *matrix_ptr = -1; 185 | } 186 | 187 | /* ### ends measuring ### */ 188 | rdtscb_getticks ( te ); 189 | 190 | } 191 | 192 | /* 193 | * MATRIX 7 194 | */ 195 | void initmatrix(){ 196 | memset (matrix, -1, sizeof(matrix)); 197 | } 198 | 199 | void inline MATRIX7 (struct timespec *ts, struct timespec *te){ 200 | register int *matrix_ptr; 201 | 202 | /* ### starts measuring ### */ 203 | rdtscb_getticks ( ts ); 204 | 205 | initmatrix(); 206 | 207 | /* ### ends measuring ### */ 208 | rdtscb_getticks ( te ); 209 | 210 | } 211 | 212 | /* 213 | * MATRIX 8 214 | */ 215 | #define init_matrix() \ 216 | memset (matrix, -1, sizeof(matrix)); 217 | 218 | void inline MATRIX8 (struct timespec *ts, struct timespec *te){ 219 | register int *matrix_ptr; 220 | 221 | /* ### starts measuring ### */ 222 | rdtscb_getticks ( ts ); 223 | 224 | init_matrix(); 225 | 226 | /* ### ends measuring ### */ 227 | rdtscb_getticks ( te ); 228 | 229 | } 230 | -------------------------------------------------------------------------------- /matrix/matrix.h: -------------------------------------------------------------------------------- 1 | /* rdtscbench: measures operations in clock cycles using RDTSC x86 instruction 2 | * 3 | * Peter Senna Tschudin - peter.senna@gmail.com 4 | * 5 | * IMPORTANT: Before running make and rdtscbench disable CPU dynamic clock. 6 | * On Linux: # service cpuspeed stop 7 | * 8 | * IMPORTANT 2: If the computer hybernates during the measurement, it may 9 | * break the measurement. 10 | * 11 | * The Makefile runs gethz.sh to cat /proc/cpuinfo looking for the CPU speed. 12 | * The result is saved in hzdef.h. See example: 13 | * $ cat /proc/cpuinfo |grep MHz 14 | * cpu MHz : 2701.000 15 | * gethz.sh converts 2701.000 to 2701000000LL and saves it at hzdef.h 16 | * 17 | */ 18 | 19 | #include "../rdtscbench/rdtscbench.h" 20 | #include /* MATRIX7 and MATRIX8 memset() */ 21 | 22 | /* Functions used to benchmark some code */ 23 | void MATRIX1 ( struct timespec *ts, struct timespec *te ); 24 | void MATRIX2 ( struct timespec *ts, struct timespec *te ); 25 | void MATRIX3 ( struct timespec *ts, struct timespec *te ); 26 | void MATRIX4 ( struct timespec *ts, struct timespec *te ); 27 | void MATRIX5 ( struct timespec *ts, struct timespec *te ); 28 | void MATRIX6 ( struct timespec *ts, struct timespec *te ); 29 | 30 | void initmatrix(); /* used by MATRIX7() */ 31 | void MATRIX7 ( struct timespec *ts, struct timespec *te ); 32 | void MATRIX8 ( struct timespec *ts, struct timespec *te ); 33 | -------------------------------------------------------------------------------- /moreExamples/Makefile: -------------------------------------------------------------------------------- 1 | NAME = moreExamples 2 | SRCDIR = ../rdtscbench 3 | CFLAGS = -lrt -lm 4 | OBJ = $(SRCDIR)/rdtscbench.o $(NAME).o 5 | 6 | 7 | all: 8 | $(MAKE) -C $(SRCDIR) 9 | $(MAKE) -C . $(NAME) 10 | 11 | $(NAME): $(OBJ) 12 | $(CC) $(CFLAGS) -o $(NAME) $(OBJ) 13 | 14 | $(SRCDIR)/rdtscbench.o: $(SRCDIR)/rdtscbench.h $(SRCDIR)/rdtscbench.c 15 | 16 | clean: 17 | rm -f *~ 18 | rm -f *.o 19 | rm -f hzdef.h 20 | rm -f $(NAME) 21 | # $(MAKE) -C $(SRCDIR) clean 22 | 23 | -------------------------------------------------------------------------------- /moreExamples/moreExamples.c: -------------------------------------------------------------------------------- 1 | /* rdtscbench: measures operations in clock cycles using RDTSC x86 instruction 2 | * 3 | * Peter Senna Tschudin - peter.senna@gmail.com 4 | * 5 | * IMPORTANT: Before running make and rdtscbench disable CPU dynamic clock. 6 | * On Linux: # service cpuspeed stop 7 | * 8 | * IMPORTANT 2: If the computer hybernates during the measurement, it may 9 | * break the measurement. 10 | * 11 | * The Makefile runs gethz.sh to cat /proc/cpuinfo looking for the CPU speed. 12 | * The result is saved in hzdef.h. See example: 13 | * $ cat /proc/cpuinfo |grep MHz 14 | * cpu MHz : 2701.000 15 | * gethz.sh converts 2701.000 to 2701000000LL and saves it at hzdef.h 16 | * 17 | */ 18 | 19 | #include "../rdtscbench/rdtscbench.h" 20 | #include 21 | #include 22 | #include 23 | #include 24 | #include 25 | #include 26 | 27 | /* Functions used to benchmark some code */ 28 | void GETTIMEOFDAY( struct timespec *ts, struct timespec *te ); 29 | void SYSCONF ( struct timespec *ts, struct timespec *te ); 30 | void SIMPLEMATH ( struct timespec *ts, struct timespec *te ); 31 | void MMEM ( struct timespec *ts, struct timespec *te ); 32 | void GMEM ( struct timespec *ts, struct timespec *te ); 33 | void PIPES ( struct timespec *ts, struct timespec *te ); 34 | 35 | int main (int argc, char *argv[]){ 36 | struct timespec begin, end; 37 | int accrepeats, repeats, detailed = 0; 38 | 39 | if ( argc < 3 ){ 40 | printf ("Error. You need to specify two numbers as arguments.\n\n %s \n\n" , argv[0]); 41 | exit (1); 42 | } 43 | repeats = atoi(argv[1]); 44 | accrepeats = atoi(argv[2]); 45 | 46 | if ( argc > 3 ) 47 | detailed = 1; 48 | 49 | rdtscb_measure ( "sysconf() ", repeats, detailed, &SYSCONF ); 50 | sleep(5); 51 | 52 | rdtscb_measure ( "gettimeofday() ", repeats, detailed, &GETTIMEOFDAY ); 53 | sleep(5); 54 | 55 | rdtscb_measure ( "malloc[1M] ", repeats, detailed, &MMEM ); 56 | sleep(5); 57 | 58 | rdtscb_measure ( "malloc[1G] ", repeats, detailed, &GMEM ); 59 | sleep(5); 60 | 61 | rdtscb_measure ( "2pipes[] ", repeats, detailed, &PIPES ); 62 | sleep(5); 63 | 64 | rdtscb_measure ( "simplemath[] ", repeats, detailed, &SIMPLEMATH ); 65 | sleep(5); 66 | 67 | rdtscb_accuracy (accrepeats, detailed ); 68 | 69 | } 70 | 71 | void inline GETTIMEOFDAY ( struct timespec *ts, struct timespec *te ){ 72 | struct timeval tv; 73 | struct timezone tz; 74 | int time; 75 | 76 | /* ### starts measuring ### */ 77 | rdtscb_getticks ( ts ); 78 | 79 | time = gettimeofday ( &tv, &tz ); 80 | 81 | /* ### ends measuring ### */ 82 | rdtscb_getticks ( te ); 83 | 84 | } 85 | 86 | 87 | void inline SIMPLEMATH ( struct timespec *ts, struct timespec *te ){ 88 | uint64_t i,a,b,c,d; 89 | a = 1, b = 2, c = 4, d = 8; 90 | 91 | /* ### starts measuring ### */ 92 | rdtscb_getticks ( ts ); 93 | 94 | for (i = 0; i < 100000LL; i++){ 95 | a = ( ( d * c ) / b); 96 | a--;b++;c++;d++; 97 | } 98 | 99 | /* ### ends measuring ### */ 100 | rdtscb_getticks ( te ); 101 | 102 | } 103 | 104 | void inline SYSCONF ( struct timespec *ts, struct timespec *te ){ 105 | long conf; 106 | 107 | /* ### starts measuring ### */ 108 | rdtscb_getticks ( ts ); 109 | 110 | conf = sysconf(_SC_ARG_MAX); 111 | 112 | /* ### ends measuring ### */ 113 | rdtscb_getticks ( te ); 114 | 115 | } 116 | 117 | void inline MMEM ( struct timespec *ts, struct timespec *te ){ 118 | unsigned int size = 1000000; 119 | char *mem; 120 | 121 | /* ### starts measuring ### */ 122 | rdtscb_getticks ( ts ); 123 | 124 | mem = malloc ( size * sizeof ( char ) ); 125 | 126 | /* ### ends measuring ### */ 127 | rdtscb_getticks ( te ); 128 | 129 | free ( mem ); 130 | } 131 | 132 | void inline GMEM ( struct timespec *ts, struct timespec *te ){ 133 | unsigned int size = 1000000000; 134 | char *mem; 135 | 136 | /* ### starts measuring ### */ 137 | rdtscb_getticks ( ts ); 138 | 139 | mem = malloc ( size * sizeof ( char ) ); 140 | 141 | /* ### ends measuring ### */ 142 | rdtscb_getticks ( te ); 143 | 144 | free ( mem ); 145 | } 146 | 147 | /* Parent writes on pipePtoC. Child reads it and writes back on pipeCtoP */ 148 | void inline PIPES ( struct timespec *ts, struct timespec *te ){ 149 | char send[16]="message", recive[16]; 150 | int pipePtoC[2], pipeCtoP[2], i; 151 | pid_t cpid; 152 | char buf; 153 | 154 | if ((pipe(pipePtoC) == -1) || (pipe(pipeCtoP) == -1)) { 155 | perror("pipe"); 156 | exit(EXIT_FAILURE); 157 | } 158 | 159 | for ( i=0; i < sizeof(recive); i++ ) 160 | recive[i] = '\0'; 161 | 162 | cpid = fork(); 163 | if (cpid == -1) { 164 | perror("fork"); 165 | exit(EXIT_FAILURE); 166 | } 167 | 168 | /* Child */ 169 | if (cpid == 0) { 170 | close(pipePtoC[1]); 171 | close(pipeCtoP[0]); 172 | 173 | /* recive message from parent and send it back */ 174 | while (read(pipePtoC[0], &buf, 1) > 0) 175 | write(pipeCtoP[1], &buf, 1); 176 | 177 | write(pipeCtoP[1], "\n", 1); 178 | 179 | close(pipePtoC[0]); 180 | close(pipeCtoP[1]); 181 | 182 | _exit(EXIT_SUCCESS); 183 | 184 | /* Parent */ 185 | } else { 186 | close(pipePtoC[0]); 187 | close(pipeCtoP[1]); 188 | 189 | /* ### starts measuring ### */ 190 | rdtscb_getticks ( ts ); 191 | 192 | /* Sendig send[16] */ 193 | write(pipePtoC[1], send, strlen(send)); 194 | close(pipePtoC[1]); 195 | 196 | /* Receiving it back and saving on recive[16]*/ 197 | i=0; 198 | while (read(pipeCtoP[0], &buf, 1) > 0){ 199 | strncat(recive, &buf, 1); 200 | i++; 201 | } 202 | recive[(i-1)]='\0'; 203 | 204 | if ( strcmp ( send, recive ) != 0 ) 205 | exit (1); 206 | 207 | /* ### ends measuring ### */ 208 | rdtscb_getticks ( te ); 209 | 210 | close(pipePtoC[0]); 211 | close(pipeCtoP[0]); 212 | 213 | wait(NULL); 214 | } 215 | } 216 | -------------------------------------------------------------------------------- /rdtscbench/Makefile: -------------------------------------------------------------------------------- 1 | CFLAGS = -lrt -lm 2 | 3 | all: gethz rdtscbench.o 4 | 5 | gethz: 6 | ./gethz.sh 7 | 8 | rdtscbench.o: rdtscbench.h rdtscbench.c 9 | 10 | clean: 11 | rm -f *~ 12 | rm -f *.o 13 | rm -f hzdef.h 14 | rm -f rdtscbench.o 15 | -------------------------------------------------------------------------------- /rdtscbench/gethz.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | # Peter Senna Tschudin - peter.senna@gmail.com 4 | 5 | clear 6 | echo 7 | echo "BEFORE RUNNING MAKE: " 8 | echo "To improve accuracy, disable cpu scalling with: # service cpuspeed stop" 9 | echo "Having constant_tsc flag at /proc/cpuinfo also improves the accuracy." 10 | echo "This is a dumb message that is shown everytime. :-)" 11 | echo 12 | 13 | rm -rf ./hzdef.h 14 | 15 | MHZ=`cat /proc/cpuinfo |grep MHz |head -n 1|cut -d ":" -f 2| cut -d " " -f 2|cut -d "." -f 1` 16 | 17 | echo -n "#define CPU_HZ $MHZ" > ./hzdef.h 18 | echo -n "000000LL" >> ./hzdef.h 19 | echo >> ./hzdef.h 20 | 21 | -------------------------------------------------------------------------------- /rdtscbench/rdtscbench.c: -------------------------------------------------------------------------------- 1 | /* rdtscbench: measures operations in clock cycles using RDTSC x86 instruction 2 | * 3 | * Peter Senna Tschudin - peter.senna@gmail.com 4 | * 5 | * IMPORTANT: Before running make and rdtscbench disable CPU dynamic clock. 6 | * On Linux: # service cpuspeed stop 7 | * 8 | * IMPORTANT 2: If the computer hybernates during the measurement, it may 9 | * break the measurement. 10 | * 11 | * The Makefile runs gethz.sh to cat /proc/cpuinfo looking for the CPU speed. 12 | * The result is saved in hzdef.h. See example: 13 | * $ cat /proc/cpuinfo |grep MHz 14 | * cpu MHz : 2701.000 15 | * gethz.sh converts 2701.000 to 2701000000LL and saves it at hzdef.h 16 | * 17 | */ 18 | 19 | #include "rdtscbench.h" 20 | 21 | /* General tool to measure run time of *function */ 22 | void inline rdtscb_measure (char msg[32], int repeats, int detailed, void (*function)( struct timespec *ts, struct timespec *te ) ){ 23 | 24 | struct timespec begin, end, diff; 25 | long double acc = 1; 26 | int i = 0; 27 | 28 | printf ("%s: ", msg); 29 | 30 | for ( i = repeats; i > 0; i-- ){ 31 | 32 | //getticks ( &begin ); 33 | function( &begin, &end ); 34 | //getticks ( &end ); 35 | 36 | diff.tv_nsec = end.tv_nsec - begin.tv_nsec; 37 | 38 | acc *= (long)(double)diff.tv_nsec; 39 | if ( detailed == 1 ) 40 | printf ( " %llu, ", diff.tv_nsec ); 41 | } 42 | long double power = 1; 43 | power /= repeats; 44 | 45 | long double valuemean = powl ( acc, power ); 46 | printf ("%25.6Lf clocks. Geom. mean of %4.0d values.\n", valuemean, repeats ); 47 | } 48 | 49 | /* Assembly code to read the TSC */ 50 | __inline__ void rdtscb_getticks(struct timespec *ts) { 51 | unsigned int a, d; 52 | 53 | asm("cpuid"); /* disables out of order execution engine */ 54 | 55 | asm volatile("rdtsc" : "=a" (a), "=d" (d)); 56 | 57 | ts->tv_nsec = (((uint64_t)a) | (((uint64_t)d) << 32)); 58 | } 59 | 60 | /* Accuracy detection 61 | * How many clock cycles in a second? It should be exactly the speed of your 62 | * CPU. Example: On a 2.7GHZ CPU it should count 2.700.000.000 clock 63 | * cycles. If it does, you got 100% accuracy. 64 | */ 65 | long double rdtscb_accuracy ( int repeats, int detailed ){ 66 | struct timespec begin, end, diff; 67 | long double accuracy = 0, acc = 1; 68 | int i; 69 | 70 | printf ("\n----------------\n\naccuracy :"); 71 | 72 | for ( i = repeats; i > 0; i-- ){ 73 | 74 | rdtscb_getticks ( &begin ); 75 | sleep ( 1 ); 76 | rdtscb_getticks ( &end ); 77 | 78 | diff.tv_nsec = (long)(double) (end.tv_nsec - begin.tv_nsec); 79 | acc *= diff.tv_nsec; 80 | if ( detailed == 1 ){ 81 | accuracy = diff.tv_nsec; 82 | accuracy *= 100; 83 | accuracy /= CPU_HZ; 84 | if ( accuracy > 100 ){ 85 | accuracy -= 100; 86 | accuracy = 100 - accuracy; 87 | } 88 | printf ( "%2.6Lf, ", accuracy ); 89 | } 90 | } 91 | long double power = 1; 92 | power /= repeats; 93 | 94 | accuracy = powl ( acc, power ); 95 | 96 | accuracy *= 100; 97 | accuracy /= CPU_HZ; 98 | 99 | if ( accuracy > 100 ){ 100 | accuracy -= 100; 101 | accuracy = 100 - accuracy; 102 | } 103 | 104 | printf (" %25.6Lf%% Geom. mean of %4.0d values.\n\n", accuracy, repeats ); 105 | 106 | return accuracy; 107 | } 108 | -------------------------------------------------------------------------------- /rdtscbench/rdtscbench.h: -------------------------------------------------------------------------------- 1 | /* rdtscbench: measures operations in clock cycles using RDTSC x86 instruction 2 | * 3 | * Peter Senna Tschudin - peter.senna@gmail.com 4 | * 5 | * IMPORTANT: Before running make and rdtscbench disable CPU dynamic clock. 6 | * On Linux: # service cpuspeed stop 7 | * 8 | * IMPORTANT 2: If the computer hybernates during the measurement, it may 9 | * break the measurement. 10 | * 11 | * The Makefile runs gethz.sh to cat /proc/cpuinfo looking for the CPU speed. 12 | * The result is saved in hzdef.h. See example: 13 | * $ cat /proc/cpuinfo |grep MHz 14 | * cpu MHz : 2701.000 15 | * gethz.sh converts 2701.000 to 2701000000LL and saves it at hzdef.h 16 | * 17 | */ 18 | 19 | #include "hzdef.h" /* for CPU_HZ */ 20 | 21 | #include 22 | #include 23 | #include 24 | #include 25 | #include 26 | #include 27 | 28 | /* 29 | * rdtscb_measure (char msg[32], int repeats, int detailed, \ 30 | * void (*function)( struct timespec *ts, struct timespec *te ) ); 31 | * 32 | * Call the function to be measured, calculate the average mean and print 33 | * results on screen. 34 | * 35 | * char msg[32] - Message to print on screen related to test 36 | * 37 | * int repeats - How many times to repeat the test? Ex.: 256 38 | * 39 | * int detailed - Print detailed information of all tests? Set to > 0 to yes. 40 | * 41 | * void (*function)( struct timespec *ts, struct timespec *te ) - Function 42 | * to be measured. See GETPID() example. 43 | * 44 | */ 45 | void rdtscb_measure (char msg[32], int repeats, int detailed, \ 46 | void (*function)( struct timespec *ts, struct timespec *te ) ); 47 | 48 | /* 49 | * rdtscb_getticks(struct timespec *ts) 50 | * saves the result of RDTSC instruction at *ts 51 | */ 52 | void rdtscb_getticks(struct timespec *ts); 53 | 54 | /* 55 | * accuracy(int repeats, int detailed); 56 | * 57 | * Calculates the accuracy of the measurement and prints it on screen. 58 | * 59 | * int repeats - How many times to repeat the test? Ex.: 8 60 | * 61 | * int detailed - Print detailed information of all tests? Set to > 0 to yes. 62 | * 63 | * RETURNS: The geometric mean of the accuracy 64 | */ 65 | long double rdtscb_accuracy(int repeats, int detailed); 66 | -------------------------------------------------------------------------------- /stacko/Makefile.noptimize: -------------------------------------------------------------------------------- 1 | NAME = stacko 2 | SRCDIR = ../rdtscbench 3 | CFLAGS = -lrt -lm 4 | OBJ = $(SRCDIR)/rdtscbench.o $(NAME).o 5 | 6 | 7 | all: 8 | $(MAKE) -C $(SRCDIR) 9 | $(MAKE) -C . $(NAME) 10 | 11 | $(NAME): $(OBJ) 12 | $(CC) $(CFLAGS) -o $(NAME) $(OBJ) 13 | 14 | $(SRCDIR)/rdtscbench.o: $(SRCDIR)/rdtscbench.h $(SRCDIR)/rdtscbench.c 15 | 16 | clean: 17 | rm -f *~ 18 | rm -f *.o 19 | rm -f hzdef.h 20 | rm -f $(NAME) 21 | # $(MAKE) -C $(SRCDIR) clean 22 | 23 | -------------------------------------------------------------------------------- /stacko/Makefile.optimize: -------------------------------------------------------------------------------- 1 | NAME = stacko 2 | SRCDIR = ../rdtscbench 3 | CFLAGS = -lrt -lm -O2 4 | OBJ = $(SRCDIR)/rdtscbench.o $(NAME).o 5 | 6 | 7 | all: 8 | $(MAKE) -C $(SRCDIR) 9 | $(MAKE) -C . $(NAME) 10 | 11 | $(NAME): $(OBJ) 12 | $(CC) $(CFLAGS) -o $(NAME) $(OBJ) 13 | 14 | $(SRCDIR)/rdtscbench.o: $(SRCDIR)/rdtscbench.h $(SRCDIR)/rdtscbench.c 15 | 16 | clean: 17 | rm -f *~ 18 | rm -f *.o 19 | rm -f hzdef.h 20 | rm -f $(NAME) 21 | # $(MAKE) -C $(SRCDIR) clean 22 | 23 | -------------------------------------------------------------------------------- /stacko/README: -------------------------------------------------------------------------------- 1 | http://stackoverflow.com/questions/4305553/optimization-tips 2 | 3 | To compile: 4 | 5 | $ mv Makefile.noptimize Makefile 6 | 7 | OR 8 | 9 | $ mv Makefile.optimize Makefile 10 | 11 | AND 12 | 13 | $ make 14 | 15 | -------------------------------------------------------------------------------- /stacko/graphs.ods: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/petersenna/rdtscbench/d5b99e7d70045361b870e8634eef20a48c68bb77/stacko/graphs.ods -------------------------------------------------------------------------------- /stacko/stacko.c: -------------------------------------------------------------------------------- 1 | /* rdtscbench: measures operations in clock cycles using RDTSC x86 instruction 2 | * 3 | * Peter Senna Tschudin - peter.senna@gmail.com 4 | * 5 | * IMPORTANT: Before running make and rdtscbench disable CPU dynamic clock. 6 | * On Linux: # service cpuspeed stop 7 | * 8 | * IMPORTANT 2: If the computer hybernates during the measurement, it may 9 | * break the measurement. 10 | * 11 | * The Makefile runs gethz.sh to cat /proc/cpuinfo looking for the CPU speed. 12 | * The result is saved in hzdef.h. See example: 13 | * $ cat /proc/cpuinfo |grep MHz 14 | * cpu MHz : 2701.000 15 | * gethz.sh converts 2701.000 to 2701000000LL and saves it at hzdef.h 16 | * 17 | */ 18 | 19 | #include "../rdtscbench/rdtscbench.h" 20 | #include 21 | #include 22 | 23 | void inline STACKO ( struct timespec *ts, struct timespec *te ); 24 | void inline STACKO1 ( struct timespec *ts, struct timespec *te ); 25 | void inline STACKO2 ( struct timespec *ts, struct timespec *te ); 26 | void inline STACKOmoonshadow ( struct timespec *ts, struct timespec *te ); 27 | void inline STACKOmoonshadow2 ( struct timespec *ts, struct timespec *te ); 28 | /*void inline STACKOonemasse ( struct timespec *ts, struct timespec *te );*/ 29 | void inline STACKOChristoffer ( struct timespec *ts, struct timespec *te ); 30 | 31 | #define MAX 100 32 | 33 | int main (int argc, char *argv[]){ 34 | struct timespec begin, end; 35 | int i, accrepeats, repeats, detailed = 0; 36 | 37 | if ( argc < 3 ){ 38 | printf ("Error. You need to specify two numbers as arguments.\n\n %s \n\n" , argv[0]); 39 | exit (1); 40 | } 41 | repeats = atoi(argv[1]); 42 | accrepeats = atoi(argv[2]); 43 | 44 | if ( argc > 3 ) 45 | detailed = 1; 46 | 47 | rdtscb_measure ( "stacko[] ", repeats, detailed, &STACKO ); 48 | sleep(3); /* optional */ 49 | rdtscb_measure ( "stacko1[] ", repeats, detailed, &STACKO1 ); 50 | sleep(3); /* optional */ 51 | rdtscb_measure ( "stacko2[] ", repeats, detailed, &STACKO2 ); 52 | sleep(3); /* optional */ 53 | rdtscb_measure ( "stackomoonshadow[] ", repeats, detailed, &STACKOmoonshadow ); 54 | sleep(3); /* optional */ 55 | rdtscb_measure ( "stackomoonshadow2[]", repeats, detailed, &STACKOmoonshadow2 ); 56 | sleep(3); /* optional */ 57 | rdtscb_measure ( "stackoChristoffer[]", repeats, detailed, &STACKOChristoffer ); 58 | sleep(3); /* optional */ 59 | 60 | /* rdtscb_measure ( "stackoonemasse[]", repeats, detailed, &STACKOonemasse );*/ 61 | 62 | 63 | 64 | /* How to use accuracy() function */ 65 | rdtscb_accuracy (accrepeats, detailed); 66 | } 67 | 68 | void inline STACKO ( struct timespec *ts, struct timespec *te ){ 69 | 70 | int i, *s, *a, *b; 71 | 72 | for (i = 0; i < MAX; ++i){ 73 | s = (int *) malloc (sizeof (int)); ++s; 74 | a = (int *) malloc (sizeof (int)); ++a; 75 | b = (int *) malloc (sizeof (int)); ++b; 76 | } 77 | 78 | srand ( 1024 ); 79 | for (i = 0; i < MAX; ++i){ 80 | a[i] = ( rand() % 2 ); 81 | b[i] = ( rand() % 2 ); 82 | } 83 | 84 | rdtscb_getticks ( ts ); /* start measurement */ 85 | 86 | for (i = 0; i < MAX; i++) 87 | s[i] = a[i] ^ b[i]; 88 | 89 | rdtscb_getticks ( te ); /* end measurement */ 90 | 91 | /* 92 | printf("\n"); 93 | for (i = 0; i < MAX; ++i) 94 | printf("%d", s[i]); 95 | printf("\n"); 96 | */ 97 | } 98 | 99 | void inline STACKO1 ( struct timespec *ts, struct timespec *te ){ 100 | 101 | register int i, *s, *a, *b; 102 | 103 | srand ( 1024 ); 104 | 105 | for (i = 0; i < MAX; ++i){ 106 | s = (int *) malloc (sizeof (int)); ++s; 107 | a = (int *) malloc (sizeof (int)); ++a; 108 | b = (int *) malloc (sizeof (int)); ++b; 109 | } 110 | 111 | srand ( 1024 ); 112 | for (i = 0; i < MAX; ++i){ 113 | a[i] = ( rand() % 2 ); 114 | b[i] = ( rand() % 2 ); 115 | } 116 | 117 | rdtscb_getticks ( ts ); /* start measurement */ 118 | 119 | for (i = 0; i < MAX; i++) 120 | s[i] = a[i] ^ b[i]; 121 | 122 | rdtscb_getticks ( te ); /* end measurement */ 123 | 124 | /* 125 | printf("\n"); 126 | for (i = 0; i < MAX; ++i) 127 | printf("%d", s[i]); 128 | printf("\n"); 129 | */ 130 | } 131 | 132 | void inline STACKO2 ( struct timespec *ts, struct timespec *te ){ 133 | 134 | register int i, *s, *s_ptr, *s_end, *a, *a_ptr, *b, *b_ptr; 135 | 136 | for (i = 0; i < MAX; ++i){ 137 | s = (int *) malloc (sizeof (int)); ++s; 138 | a = (int *) malloc (sizeof (int)); ++a; 139 | b = (int *) malloc (sizeof (int)); ++b; 140 | } 141 | 142 | srand ( 1024 ); 143 | for (i = 0; i < MAX; ++i){ 144 | a[i] = ( rand() % 2 ); 145 | b[i] = ( rand() % 2 ); 146 | } 147 | 148 | rdtscb_getticks ( ts ); /* start measurement */ 149 | 150 | s_end = &s[MAX]; 151 | for (s_ptr = &s[0], a_ptr = &a[0], b_ptr = &b[0]; s_ptr < s_end; ++s_ptr, ++a_ptr, ++b_ptr) 152 | *s_ptr = *a_ptr ^ *b_ptr; 153 | 154 | rdtscb_getticks ( te ); /* end measurement */ 155 | 156 | /* 157 | printf("\n"); 158 | for (i = 0; i < MAX; ++i) 159 | printf("%d", s[i]); 160 | printf("\n"); 161 | */ 162 | } 163 | 164 | void inline STACKOmoonshadow ( struct timespec *ts, struct timespec *te ){ 165 | 166 | int i, *s, *s_end, *a, *b, *s_ptr, *a_ptr, *b_ptr; 167 | 168 | for (i = 0; i < MAX; ++i){ 169 | s = (int *) malloc (sizeof (int)); ++s; 170 | a = (int *) malloc (sizeof (int)); ++a; 171 | b = (int *) malloc (sizeof (int)); ++b; 172 | } 173 | 174 | srand ( 1024 ); 175 | for (i = 0; i < MAX; ++i){ 176 | a[i] = ( rand() % 2 ); 177 | b[i] = ( rand() % 2 ); 178 | } 179 | 180 | rdtscb_getticks ( ts ); /* start measurement */ 181 | 182 | s_ptr = &s[0]; 183 | a_ptr = &a[0]; 184 | b_ptr = &b[0]; 185 | 186 | for (i = 0; i < (MAX/4); i++){ 187 | 188 | s_ptr[0] = a_ptr[0] ^ b_ptr[0]; 189 | s_ptr[1] = a_ptr[1] ^ b_ptr[1]; 190 | s_ptr[2] = a_ptr[2] ^ b_ptr[2]; 191 | s_ptr[3] = a_ptr[3] ^ b_ptr[3]; 192 | s_ptr+=4; a_ptr+=4; b_ptr+=4; 193 | } 194 | 195 | rdtscb_getticks ( te ); /* end measurement */ 196 | 197 | /* 198 | printf("\n"); 199 | for (i = 0; i < MAX; ++i) 200 | printf("%d", s[i]); 201 | printf("\n"); 202 | */ 203 | } 204 | 205 | void inline STACKOmoonshadow2 ( struct timespec *ts, struct timespec *te ){ 206 | 207 | register int i, *s, *s_end, *a, *b, *s_ptr, *a_ptr, *b_ptr; 208 | 209 | for (i = 0; i < MAX; ++i){ 210 | s = (int *) malloc (sizeof (int)); ++s; 211 | a = (int *) malloc (sizeof (int)); ++a; 212 | b = (int *) malloc (sizeof (int)); ++b; 213 | } 214 | 215 | srand ( 1024 ); 216 | for (i = 0; i < MAX; ++i){ 217 | a[i] = ( rand() % 2 ); 218 | b[i] = ( rand() % 2 ); 219 | } 220 | 221 | rdtscb_getticks ( ts ); /* start measurement */ 222 | 223 | s_ptr = &s[0]; 224 | a_ptr = &a[0]; 225 | b_ptr = &b[0]; 226 | 227 | for (i = 0; i < (MAX/4); i++){ 228 | 229 | s_ptr[0] = a_ptr[0] ^ b_ptr[0]; 230 | s_ptr[1] = a_ptr[1] ^ b_ptr[1]; 231 | s_ptr[2] = a_ptr[2] ^ b_ptr[2]; 232 | s_ptr[3] = a_ptr[3] ^ b_ptr[3]; 233 | s_ptr+=4; a_ptr+=4; b_ptr+=4; 234 | } 235 | 236 | rdtscb_getticks ( te ); /* end measurement */ 237 | 238 | /* 239 | printf("\n"); 240 | for (i = 0; i < MAX; ++i) 241 | printf("%d", s[i]); 242 | printf("\n"); 243 | */ 244 | } 245 | 246 | void inline STACKOChristoffer ( struct timespec *ts, struct timespec *te ){ 247 | 248 | int i, *s, *s_end, *a, *b; 249 | 250 | for (i = 0; i < MAX; ++i){ 251 | s = (int *) malloc (sizeof (int)); ++s; 252 | a = (int *) malloc (sizeof (int)); ++a; 253 | b = (int *) malloc (sizeof (int)); ++b; 254 | } 255 | 256 | srand ( 1024 ); 257 | for (i = 0; i < MAX; ++i){ 258 | a[i] = ( rand() % 2 ); 259 | b[i] = ( rand() % 2 ); 260 | } 261 | 262 | rdtscb_getticks ( ts ); /* start measurement */ 263 | 264 | #pragma omp for 265 | for (i = 0; i < MAX; i++) 266 | { 267 | s[i] = a[i] ^ b[i]; 268 | } 269 | 270 | rdtscb_getticks ( te ); /* end measurement */ 271 | /* 272 | printf("\n"); 273 | for (i = 0; i < MAX; ++i) 274 | printf("%d", s[i]); 275 | printf("\n"); 276 | */ 277 | } 278 | 279 | /* 280 | void inline STACKOonemasse ( struct timespec *ts, struct timespec *te ){ 281 | 282 | register int i, *s, *a, *b; 283 | 284 | for (i = 0; i < MAX; ++i){ 285 | s = (int *) malloc (sizeof (int)); ++s; 286 | a = (int *) malloc (sizeof (int)); ++a; 287 | b = (int *) malloc (sizeof (int)); ++b; 288 | } 289 | 290 | srand ( 1024 ); 291 | for (i = 0; i < MAX; ++i){ 292 | s[i] = a[i] = ( rand() % 2 ); 293 | b[i] = ( rand() % 2 ); 294 | } 295 | 296 | rdtscb_getticks ( ts ); 297 | 298 | memcpy( s, a, MAX ); 299 | 300 | for (i = 0; i < MAX; i++) 301 | { 302 | s[i] = s[i] ^ b[i]; 303 | } 304 | 305 | rdtscb_getticks ( te ); 306 | 307 | printf("\n"); 308 | for (i = 0; i < MAX; ++i) 309 | printf("%d", s[i]); 310 | printf("\n"); 311 | } 312 | */ 313 | --------------------------------------------------------------------------------