Calculation of CPU cycles in Zyphyr OS

Zyphyr OS provides k_cycles_get_32() and k_cycles_get_64() APIs. k_cycles_get_32() and k_cycles_get_64() used to obtain the current cycle count from the hardware timer. Both functions are designed to provide high-resolution timing by accessing the fastest available hardware timer. These functions are used for benchmarking or profiling the critical sections of the code.

k_cycles_get_32() returns the 32 bit value representing current CPU cycles. This value wraps around after reaching the max value of 0xFFFFFFFF.

k_cycles_get_64() returns the 64 bit value representing the current CPU cycles if available. This function is available only if the hardware supports the 64-bit cycle counter and CONFIG_TIMER_HAS_64BIT_CYCLE_COUNTER configuration option is enabled.

sys_clock_hw_cycles_per_sec() returns the frequency of the hardware timer used for the system clock measured in cycles per second. This function is used to convert the cycle counts to milliseconds or microseconds or seconds.

The following code snippet can be used to calculate the CPU cycles of a specific code which is having a while loop:

{
	uint32_t scycles, ecycles, tot_sec = 0, tot_cycles = 0;
	static uint32_t cnt=1;
	scycles =  k_cycle_get_32();

	while (...) {
		//... actual code ...
		ecycles = k_cycle_get_32();
		tot_sec = add_cycles((ecycles>scycles) ? ecycles-scycles : scycles + (0xffffffff - ecycles),
								&tot_cycles, tot_sec);
		scycles = ecycles;
	}

	printf("\r\n %d %s : cycles %u * (clock speed %x) + %u, secs %d, msecs %d \n",
		__LINE__, __func__, tot_sec, sys_clock_hw_cycles_per_sec(), tot_cycles, tot_sec,
		 (tot_cycles*1000)/sys_clock_hw_cycles_per_sec());

}

uint32_t add_cycles(uint32_t cycles, uint32_t *tot_cycles, uint32_t tot_sec){
	*tot_cycles +=  cycles;
	uint32_t cpu_freq =  sys_clock_hw_cycles_per_sec();
	if (*tot_cycles > cpu_freq) {
		tot_sec ++;
		*tot_cycles %= (cpu_freq);
	}
	return tot_sec;
}        

References:

Kernel Timing — Zephyr Project Documentation


#k_cycle_get_32 #zephyr #jyo_services

要查看或添加评论,请登录

Jyothi Vemulapalli的更多文章

  • SysTick in ARM-Cortext M

    SysTick in ARM-Cortext M

    SysTick is a 24-bit system timer integrated into ARM Cortex-M processors, designed for generating periodic interrupts…

  • ARM exception levels and execution states

    ARM exception levels and execution states

    ARM Exception levels The term for privilege in AArch64 architecture is "Exception Level" (EL), with levels numbered EL0…

  • Pawanmuktasana Part 3 (Shakti Bandha asanas)

    Pawanmuktasana Part 3 (Shakti Bandha asanas)

    These practices aim to improve energy flow, release neuro-muscular knots in the pelvic region and spine, and enhance…

  • fpu, arch, stack protection compilation options

    fpu, arch, stack protection compilation options

    -mfpu The -mfpu option in GCC (GNU Compiler Collection) specifies the floating-point unit (FPU) used for floating-point…

  • My learnings on bitbake

    My learnings on bitbake

    Bitbake is a task execution tool primarily used in the Yocto Project for creating custom Linux distributions and…

  • Pawanamuktasana series 2

    Pawanamuktasana series 2

    Pawanamuktasana series 2 enhances digestive systems and helps remove abdominal energy blockages. These asanas are…

  • Pawanamuktasana Series?1

    Pawanamuktasana Series?1

    Pawanmuktasana series is very useful as a preparatory practice for anyone, as it opens up all the major joints and…

  • Asanas

    Asanas

    “Sthiram sukham aasanam”, meaning that position which is comfortable and steady. Asanas are specific body positions…

  • const int const *ptr

    const int const *ptr

    const keyword The const keyword is used to declare variables whose value cant be changed after initialisation, it is…

  • Monolithic to Microservices or Microservices to Monolithic

    Monolithic to Microservices or Microservices to Monolithic

    Monolithic architecture is a traditional approach where all the components of an application are tightly coupled and…

社区洞察

其他会员也浏览了