# CPU cycle count

**URL:** https://forums.sifive.com/t/cpu-cycle-count/3014
**Category:** RISC-V
**Created:** [November 6, 2019, 8:47am UTC](https://forums.sifive.com/t/cpu-cycle-count/3014 "2019-11-06T08:47:58Z")
**Posts on this page:** 9
**Page:** 1

<div class="post-metadata">

### Author: ![UV\_REDDY](https://avatars.discourse-cdn.com/v4/letter/u/b5a626/32.png) [@UV\_REDDY](https://forums.sifive.com/u/UV_REDDY)
#### Post date: [November 6, 2019, 8:47am UTC](https://forums.sifive.com/t/cpu-cycle-count/3014/1 "2019-11-06T08:47:58Z")

</div>

Kindly clarify the below mentioned issues:

1. For RV32G variant, I want to find how many CPU cycles are required for execution of each instruction

I have searched a lot to find out that how many clock is needed for operation of each instruction in ISA RV32G variant, but I couldn’t find any thing.

1. The base timing information is for RV32G variant, for determining the CPU cycle count. Please do provide the base timing information for RV32G variant of SiFive products

Thanks in advance

---

<div class="post-metadata">

### Author: ![bruce](https://sea2.discourse-cdn.com/flex020/user_avatar/forums.sifive.com/bruce/32/452_2.png) [@bruce](https://forums.sifive.com/u/bruce)
#### Post date: [November 6, 2019, 9:50am UTC](https://forums.sifive.com/t/cpu-cycle-count/3014/2 "2019-11-06T09:50:28Z")

</div>

The ISA does not specify the CPU cycles for each instruction. There are many possible ways to build a CPU that executes the RISC-V instruction set, depending on what trade-off you want in core size, power, speed, cost etc. Some such as Olof Kindgren’s award-winning “SERV” bit-serial FPGA core take several dozen clock cycles per instruction. Others such as SiFive’s superscalar 7-series and 8-series cores can execute on average more than one instruction per clock cycle.

In general, SiFive’s cores execute one instruction per clock cycle per pipeline, if a suitable instruction is available for dispatch, if required inputs are available. There can be delays if loads do not hit in cache or TIM, or if branches are mispredicted. You need to read the datasheet for the core you are interested in to get information, and even then only some cores in some specially-configured deterministic configurations produce guaranteed execution times.

RISC-V provides performance counter CSRs so that you can measure the number of instructions executed and number of clock cycles actually used by your code. SiFive cores can be configured to have additional performance monitoring counters such as cache misses, pipeline interlocks, branch mispredicts and so forth.

---

<div class="post-metadata">

### Author: ![jimw](https://avatars.discourse-cdn.com/v4/letter/j/f14d63/32.png) [@jimw](https://forums.sifive.com/u/jimw)
#### Post date: [November 6, 2019, 6:28pm UTC](https://forums.sifive.com/t/cpu-cycle-count/3014/3 "2019-11-06T18:28:52Z")

</div>

There is a chapter in our processor docs that gives pipeline info, though it doesn’t explain all of the details. You can find the processor docs at [sifive.com/documentation](http://sifive.com/documentation)

You can also find some info by looking at the gcc sources, in the pipeline description files, and the RTX\_COST macros, but these are approximations of the hardware. See for instance [https://gcc.gnu.org/git/?p=gcc.git;a=blob;f=gcc/config/riscv/generic.md;h=fcd78b990feccbdceeee70df8de2cd1341757744;hb=HEAD](https://gcc.gnu.org/git/?p=gcc.git;a=blob;f=gcc/config/riscv/generic.md;h=fcd78b990feccbdceeee70df8de2cd1341757744;hb=HEAD)  
This is for rocket-chip which our 3 and 5 series parts are based on. There is also a sifive-7-series.md file. There is no pipeline description file for the 2 or 8 series parts yet.

---

<div class="post-metadata">

### Author: ![UV\_REDDY](https://avatars.discourse-cdn.com/v4/letter/u/b5a626/32.png) [@UV\_REDDY](https://forums.sifive.com/u/UV_REDDY)
#### Post date: [November 8, 2019, 7:00am UTC](https://forums.sifive.com/t/cpu-cycle-count/3014/4 "2019-11-08T07:00:07Z")

</div>

Respected sir,

I have gone through the list of SiFive’s RISCV products. And found that Freedom U54 is based on RV64GC, which is of my concern.  
Could you help me in this regard, for providing the base timing information of RV32GC for U54.

Thanks and Regards

---

<div class="post-metadata">

### Author: ![bruce](https://sea2.discourse-cdn.com/flex020/user_avatar/forums.sifive.com/bruce/32/452_2.png) [@bruce](https://forums.sifive.com/u/bruce)
#### Post date: [November 8, 2019, 9:30am UTC](https://forums.sifive.com/t/cpu-cycle-count/3014/5 "2019-11-08T09:30:13Z")

</div>

Jim pointed you to the documentation.

The manual for the U54 is here

> **[sifive%2F449c97ba-41e6-4b70-b522-8ddde5d3a34e\_sifive+u54+manual+v19.08p0p1.pdf](https://sifive.cdn.prismic.io/sifive%2F449c97ba-41e6-4b70-b522-8ddde5d3a34e_sifive+u54+manual+v19.08p0p1.pdf)**
>
> 583.60 KB

Section 3 has the information you want.

---

<div class="post-metadata">

### Author: ![UV\_REDDY](https://avatars.discourse-cdn.com/v4/letter/u/b5a626/32.png) [@UV\_REDDY](https://forums.sifive.com/u/UV_REDDY)
#### Post date: [November 13, 2019, 5:29am UTC](https://forums.sifive.com/t/cpu-cycle-count/3014/6 "2019-11-13T05:29:56Z")

</div>

Thank you sir,  
I have gone through the documentation of sifive u54.  
But it contains the timing information of only RISCV base instructions. I want to know the number of cycles consumed for floating and compression instructions of RV32GC. Could anyone suggest me in this regard.

---

<div class="post-metadata">

### Author: ![godinez](https://sea2.discourse-cdn.com/flex020/user_avatar/forums.sifive.com/godinez/32/1076_2.png) [@godinez](https://forums.sifive.com/u/godinez)
#### Post date: [April 24, 2020, 1:27pm UTC](https://forums.sifive.com/t/cpu-cycle-count/3014/7 "2020-04-24T13:27:31Z")

</div>

Hi Bruce,

I am looking for the most accurate way to measure the required cycles (time) for some applications I am benchmarking on the SiFive FE310-G002 available on the HiFive1 rev B. By looking at the Dhrystone, I found 3 different ways, but I am looking how to access, at C level not at assembly level, the performance counters. Can you point me to some useful information about this?

Best regards,  
Jorge

---

<div class="post-metadata">

### Author: ![Jim](https://sea2.discourse-cdn.com/flex020/user_avatar/forums.sifive.com/jim/32/632_2.png) [@Jim](https://forums.sifive.com/u/Jim)
#### Post date: [April 24, 2020, 5:02pm UTC](https://forums.sifive.com/t/cpu-cycle-count/3014/8 "2020-04-24T17:02:42Z")

</div>

Please see this C example for how to use the Hardware Performance Monitors:

> **[sifive/example-hpm](https://github.com/sifive/example-hpm)**
>
> Demonstrates usage of the RISC-V hardware performance counter APIs. - sifive/example-hpm

---

<div class="post-metadata">

### Author: ![godinez](https://sea2.discourse-cdn.com/flex020/user_avatar/forums.sifive.com/godinez/32/1076_2.png) [@godinez](https://forums.sifive.com/u/godinez)
#### Post date: [April 27, 2020, 1:57pm UTC](https://forums.sifive.com/t/cpu-cycle-count/3014/9 "2020-04-27T13:57:19Z")

</div>

Hi Jim,

thank you very much for pointing me to this file. Then I realized that the HPM support is available in other branch rather than in the default one (v201908-branch), which I was using. Now I am using the master and I was able to compile and run this example. However, I notice in this example-hpm that the METAL\_HPM\_CYCLE counter is read but never set and for that reason I always get 0 (zero). I put some additional dummy code and I was able to see changes in the other two counters set and read in this example-hpm.

What parameters should I use to proper set the METAL\_HPM\_CYCLE counter? I see the macros for events and classes in hpm.h, but I don’t know what they mean. Could please point me to additional documentation to better understand this.

Best regards,  
Jorge
