[arch] tweak arch_cycle_count prototype to return a ulong

This lets some arches return a 64bit counter.

As a result of fixing this, removed -Wno-format switch in the test app
which caused the need to fix a lot of printfs.
This commit is contained in:
Travis Geiselbrecht
2020-05-16 17:55:50 -07:00
parent 35c3742b6c
commit 80967e78a6
17 changed files with 70 additions and 65 deletions

View File

@@ -28,13 +28,13 @@ __NO_INLINE static void bench_set_overhead(void) {
return;
}
uint count = arch_cycle_count();
ulong count = arch_cycle_count();
for (uint i = 0; i < ITER; i++) {
__asm__ volatile("");
}
count = arch_cycle_count() - count;
printf("took %u cycles overhead to loop %u times\n",
printf("took %lu cycles overhead to loop %u times\n",
count, ITER);
free(buf);
@@ -47,13 +47,13 @@ __NO_INLINE static void bench_memset(void) {
return;
}
uint count = arch_cycle_count();
ulong count = arch_cycle_count();
for (uint i = 0; i < ITER; i++) {
memset(buf, 0, BUFSIZE);
}
count = arch_cycle_count() - count;
printf("took %u cycles to memset a buffer of size %u %d times (%u bytes), %f bytes/cycle\n",
printf("took %lu cycles to memset a buffer of size %zu %d times (%zu bytes), %f bytes/cycle\n",
count, BUFSIZE, ITER, BUFSIZE * ITER, (BUFSIZE * ITER) / (float)count);
free(buf);
@@ -68,7 +68,7 @@ __NO_INLINE static void bench_cset_##type(void) \
return; \
} \
\
uint count = arch_cycle_count(); \
ulong count = arch_cycle_count(); \
for (uint i = 0; i < ITER; i++) { \
for (uint j = 0; j < BUFSIZE / sizeof(*buf); j++) { \
buf[j] = 0; \
@@ -76,7 +76,7 @@ __NO_INLINE static void bench_cset_##type(void) \
} \
count = arch_cycle_count() - count; \
\
printf("took %u cycles to manually clear a buffer using wordsize %d of size %u %d times (%u bytes), %f bytes/cycle\n", \
printf("took %lu cycles to manually clear a buffer using wordsize %zu of size %zu %u times (%zu bytes), %f bytes/cycle\n", \
count, sizeof(*buf), BUFSIZE, ITER, BUFSIZE * ITER, (BUFSIZE * ITER) / (float)count); \
\
free(buf); \
@@ -94,7 +94,7 @@ __NO_INLINE static void bench_cset_wide(void) {
return;
}
uint count = arch_cycle_count();
ulong count = arch_cycle_count();
for (uint i = 0; i < ITER; i++) {
for (uint j = 0; j < BUFSIZE / sizeof(*buf) / 8; j++) {
buf[j*8] = 0;
@@ -109,7 +109,7 @@ __NO_INLINE static void bench_cset_wide(void) {
}
count = arch_cycle_count() - count;
printf("took %u cycles to manually clear a buffer of size %u %d times 8 words at a time (%u bytes), %f bytes/cycle\n",
printf("took %lu cycles to manually clear a buffer of size %zu %d times 8 words at a time (%zu bytes), %f bytes/cycle\n",
count, BUFSIZE, ITER, BUFSIZE * ITER, (BUFSIZE * ITER) / (float)count);
free(buf);
@@ -122,13 +122,13 @@ __NO_INLINE static void bench_memcpy(void) {
return;
}
uint count = arch_cycle_count();
ulong count = arch_cycle_count();
for (uint i = 0; i < ITER; i++) {
memcpy(buf, buf + BUFSIZE / 2, BUFSIZE / 2);
}
count = arch_cycle_count() - count;
printf("took %u cycles to memcpy a buffer of size %u %d times (%u source bytes), %f source bytes/cycle\n",
printf("took %lu cycles to memcpy a buffer of size %zu %d times (%zu source bytes), %f source bytes/cycle\n",
count, BUFSIZE / 2, ITER, BUFSIZE / 2 * ITER, (BUFSIZE / 2 * ITER) / (float)count);
free(buf);
@@ -142,7 +142,7 @@ __NO_INLINE static void arm_bench_cset_stm(void) {
return;
}
uint count = arch_cycle_count();
ulong count = arch_cycle_count();
for (uint i = 0; i < ITER; i++) {
for (uint j = 0; j < BUFSIZE / sizeof(*buf) / 8; j++) {
__asm__ volatile(
@@ -153,7 +153,7 @@ __NO_INLINE static void arm_bench_cset_stm(void) {
}
count = arch_cycle_count() - count;
printf("took %u cycles to manually clear a buffer of size %u %d times 8 words at a time using stm (%u bytes), %f bytes/cycle\n",
printf("took %lu cycles to manually clear a buffer of size %zu %d times 8 words at a time using stm (%zu bytes), %f bytes/cycle\n",
count, BUFSIZE, ITER, BUFSIZE * ITER, (BUFSIZE * ITER) / (float)count);
free(buf);
@@ -161,7 +161,7 @@ __NO_INLINE static void arm_bench_cset_stm(void) {
#if (__CORTEX_M >= 0x03)
__NO_INLINE static void arm_bench_multi_issue(void) {
uint32_t cycles;
ulong cycles;
uint32_t a = 0, b = 0, c = 0, d = 0, e = 0, f = 0, g = 0, h = 0;
#define ITER 1000000
uint count = ITER;
@@ -179,7 +179,7 @@ __NO_INLINE static void arm_bench_multi_issue(void) {
}
cycles = arch_cycle_count() - cycles;
printf("took %u cycles to issue 8 integer ops (%f cycles/iteration)\n", cycles, (float)cycles / ITER);
printf("took %lu cycles to issue 8 integer ops (%f cycles/iteration)\n", cycles, (float)cycles / ITER);
#undef ITER
}
#endif // __CORTEX_M
@@ -192,35 +192,35 @@ __NO_INLINE static void bench_sincos(void) {
printf("touching the floating point unit\n");
__UNUSED volatile double _hole = sin(0);
uint count = arch_cycle_count();
ulong count = arch_cycle_count();
__UNUSED double a = sin(2.0);
count = arch_cycle_count() - count;
printf("took %u cycles for sin()\n", count);
printf("took %lu cycles for sin()\n", count);
count = arch_cycle_count();
a = cos(2.0);
count = arch_cycle_count() - count;
printf("took %u cycles for cos()\n", count);
printf("took %lu cycles for cos()\n", count);
count = arch_cycle_count();
a = sinf(2.0);
count = arch_cycle_count() - count;
printf("took %u cycles for sinf()\n", count);
printf("took %lu cycles for sinf()\n", count);
count = arch_cycle_count();
a = cosf(2.0);
count = arch_cycle_count() - count;
printf("took %u cycles for cosf()\n", count);
printf("took %lu cycles for cosf()\n", count);
count = arch_cycle_count();
a = sqrt(1234567.0);
count = arch_cycle_count() - count;
printf("took %u cycles for sqrt()\n", count);
printf("took %lu cycles for sqrt()\n", count);
count = arch_cycle_count();
a = sqrtf(1234567.0f);
count = arch_cycle_count() - count;
printf("took %u cycles for sqrtf()\n", count);
printf("took %lu cycles for sqrtf()\n", count);
}
#endif // WITH_LIB_LIBM

View File

@@ -9,19 +9,19 @@
#define ASSERT_EQ(a, b) \
do { \
int _a = (a); \
int _b = (b); \
typeof(a) _a = (a); \
typeof(b) _b = (b); \
if (_a != _b) { \
panic("%d != %d (%s:%d)\n", a, b, __FILE__, __LINE__); \
panic("%lu != %lu (%s:%d)\n", (ulong)a, (ulong)b, __FILE__, __LINE__); \
} \
} while (0);
#define ASSERT_LEQ(a, b) \
do { \
int _a = (a); \
int _b = (b); \
typeof(a) _a = (a); \
typeof(b) _b = (b); \
if (_a > _b) { \
panic("%d not <= %d (%s:%d)\n", a, b, __FILE__, __LINE__); \
panic("%lu not <= %lu (%s:%d)\n", (ulong)a, (ulong)b, __FILE__, __LINE__); \
} \
} while (0);
@@ -32,33 +32,33 @@ int cbuf_tests(int argc, const cmd_args *argv) {
cbuf_initialize(&cbuf, 16);
ASSERT_EQ(15, cbuf_space_avail(&cbuf));
ASSERT_EQ(15UL, cbuf_space_avail(&cbuf));
ASSERT_EQ(8, cbuf_write(&cbuf, "abcdefgh", 8, false));
ASSERT_EQ(8UL, cbuf_write(&cbuf, "abcdefgh", 8, false));
ASSERT_EQ(7, cbuf_space_avail(&cbuf));
ASSERT_EQ(7UL, cbuf_space_avail(&cbuf));
// Only 7 bytes should fit since if we write all 16 bytes,
// head == tail and we can't distinguish it from the start case.
ASSERT_EQ(7, cbuf_write(&cbuf, "ijklmnop", 8, false));
ASSERT_EQ(7UL, cbuf_write(&cbuf, "ijklmnop", 8, false));
ASSERT_EQ(0, cbuf_space_avail(&cbuf));
ASSERT_EQ(0UL, cbuf_space_avail(&cbuf));
// Nothing should fit.
ASSERT_EQ(0, cbuf_write(&cbuf, "XXXXXXXX", 8, false));
ASSERT_EQ(0UL, cbuf_write(&cbuf, "XXXXXXXX", 8, false));
ASSERT_EQ(0, cbuf_space_avail(&cbuf));
ASSERT_EQ(0UL, cbuf_space_avail(&cbuf));
// Read a few bytes.
{
char buf[32];
ASSERT_EQ(3, cbuf_read(&cbuf, buf, 3, false));
ASSERT_EQ(3UL, cbuf_read(&cbuf, buf, 3, false));
for (int i = 0; i < 3; ++i) {
ASSERT_EQ(buf[i], 'a' + i);
}
// Try reading 32 bytes.
ASSERT_EQ(12, cbuf_read(&cbuf, buf, 32, false));
ASSERT_EQ(12UL, cbuf_read(&cbuf, buf, 32, false));
for (int i = 0; i < 12; ++i) {
ASSERT_EQ(buf[i], 'd' + i);
}
@@ -66,7 +66,7 @@ int cbuf_tests(int argc, const cmd_args *argv) {
cbuf_reset(&cbuf);
ASSERT_EQ(15, cbuf_space_avail(&cbuf));
ASSERT_EQ(15UL, cbuf_space_avail(&cbuf));
// Random tests. Keep writing in random chunks up to 8 bytes, then
// reading in chunks up to 8 bytes. Verify values.

View File

@@ -16,7 +16,7 @@
#include <platform.h>
int clock_tests(int argc, const cmd_args *argv) {
uint32_t c;
ulong c;
lk_time_t t;
lk_bigtime_t t2;
@@ -27,7 +27,7 @@ int clock_tests(int argc, const cmd_args *argv) {
t = current_time();
}
c = arch_cycle_count() - c;
printf("%u cycles per current_time()\n", c / CYCLE_COUNT_TRIES);
printf("%lu cycles per current_time()\n", c / CYCLE_COUNT_TRIES);
thread_sleep(100);
c = arch_cycle_count();
@@ -35,7 +35,7 @@ int clock_tests(int argc, const cmd_args *argv) {
t2 = current_time_hires();
}
c = arch_cycle_count() - c;
printf("%u cycles per current_time_hires()\n", c / CYCLE_COUNT_TRIES);
printf("%lu cycles per current_time_hires()\n", c / CYCLE_COUNT_TRIES);
printf("making sure time never goes backwards\n");
{
@@ -46,7 +46,7 @@ int clock_tests(int argc, const cmd_args *argv) {
t = current_time();
//printf("%lu %lu\n", last, t);
if (TIME_LT(t, last)) {
printf("WARNING: time ran backwards: %lu < %lu\n", t, last);
printf("WARNING: time ran backwards: %u < %u\n", t, last);
last = t;
continue;
}
@@ -80,7 +80,7 @@ int clock_tests(int argc, const cmd_args *argv) {
t = current_time();
t2 = current_time_hires();
if (t > ((t2 + 500) / 1000)) {
printf("WARNING: current_time() ahead of current_time_hires() %lu %llu\n", t, t2);
printf("WARNING: current_time() ahead of current_time_hires() %u %llu\n", t, t2);
}
if (t - start > 5000)
break;
@@ -95,12 +95,12 @@ int clock_tests(int argc, const cmd_args *argv) {
printf("measuring cpu clock against current_time_hires()\n");
for (int i = 0; i < 5; i++) {
uint cycles = arch_cycle_count();
ulong cycles = arch_cycle_count();
lk_bigtime_t start = current_time_hires();
while ((current_time_hires() - start) < 1000000)
;
cycles = arch_cycle_count() - cycles;
printf("%u cycles per second\n", cycles);
printf("%lu cycles per second\n", cycles);
}
return NO_ERROR;

View File

@@ -29,13 +29,13 @@ static int fibo_thread(void *argv) {
snprintf(name, sizeof(name), "fibo %lu", fibo - 1);
t[0] = thread_create(name, &fibo_thread, (void *)(fibo - 1), DEFAULT_PRIORITY, DEFAULT_STACK_SIZE);
if (!t[0]) {
printf("error creating thread for fibo %d\n", fibo-1);
printf("error creating thread for fibo %ld\n", fibo-1);
return 0;
}
snprintf(name, sizeof(name), "fibo %lu", fibo - 2);
t[1] = thread_create(name, &fibo_thread, (void *)(fibo - 2), DEFAULT_PRIORITY, DEFAULT_STACK_SIZE);
if (!t[1]) {
printf("error creating thread for fibo %d\n", fibo-2);
printf("error creating thread for fibo %ld\n", fibo-2);
thread_resume(t[0]);
thread_join(t[0], NULL, INFINITE_TIME);
return 0;

View File

@@ -77,7 +77,7 @@ static void float_tests(void) {
thread_t *t[8];
FLOAT val[countof(t)];
printf("creating %u floating point threads\n", countof(t));
printf("creating %zu floating point threads\n", countof(t));
for (uint i = 0; i < countof(t); i++) {
val[i] = i;
char name[32];

View File

@@ -193,7 +193,7 @@ usage:
#endif
printf("got buffer at %p of length 0x%lx\n", ptr, len);
printf("got buffer at %p of length %#zx\n", ptr, len);
/* run the tests */
do_mem_tests(ptr, len);

View File

@@ -11,6 +11,10 @@
#include <string.h>
#include <lk/debug.h>
// We're doing a few things here that the compiler doesn't like, so disable printf warnings
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wformat"
int printf_tests(int argc, const cmd_args *argv) {
printf("printf tests\n");
@@ -117,4 +121,5 @@ int printf_tests_float(int argc, const cmd_args *argv) {
return NO_ERROR;
}
#pragma GCC diagnostic pop

View File

@@ -22,6 +22,6 @@ MODULE_ARM_OVERRIDE_SRCS := \
MODULE_DEPS += \
lib/cbuf
MODULE_COMPILEFLAGS += -Wno-format -fno-builtin
MODULE_COMPILEFLAGS += -fno-builtin
include make/module.mk

View File

@@ -80,7 +80,7 @@ static inline bool arch_in_int_handler(void) {
#endif
}
static inline uint32_t arch_cycle_count(void) {
static inline ulong arch_cycle_count(void) {
#if ARM_ISA_ARMV7M
#if ENABLE_CYCLE_COUNTER
#define DWT_CYCCNT (0xE0001004)
@@ -175,7 +175,7 @@ static inline bool arch_ints_disabled(void) {
return !!state;
}
static inline uint32_t arch_cycle_count(void) {
static inline ulong arch_cycle_count(void) {
return 0;
}

View File

@@ -69,7 +69,7 @@ static inline bool arch_fiqs_disabled(void) {
#define smp_rmb() CF
#endif
static inline uint32_t arch_cycle_count(void) {
static inline ulong arch_cycle_count(void) {
//#warning no arch_cycle_count implementation
return 0;
}

View File

@@ -22,7 +22,7 @@ static void arch_disable_ints(void);
static bool arch_ints_disabled(void);
static bool arch_in_int_handler(void);
static uint32_t arch_cycle_count(void);
static ulong arch_cycle_count(void);
static uint arch_curr_cpu_num(void);

View File

@@ -60,7 +60,7 @@ static inline void arch_set_current_thread(struct thread *t) {
_current_thread = t;
}
static inline uint32_t arch_cycle_count(void) { return 0; }
static inline ulong arch_cycle_count(void) { return 0; }
static inline uint arch_curr_cpu_num(void) {
return 0;

View File

@@ -51,7 +51,7 @@ static inline void arch_set_current_thread(struct thread *t) {
_current_thread = t;
}
static inline uint32_t arch_cycle_count(void) { return 0; }
static inline ulong arch_cycle_count(void) { return 0; }
static inline uint arch_curr_cpu_num(void) {
return 0;

View File

@@ -74,7 +74,7 @@ static inline void arch_set_current_thread(struct thread *t) {
_current_thread = t;
}
static inline uint32_t arch_cycle_count(void) { return 0; }
static inline ulong arch_cycle_count(void) { return 0; }
static inline uint arch_curr_cpu_num(void) {
return 0;

View File

@@ -37,7 +37,7 @@ static inline void arch_set_current_thread(struct thread *t) {
__current_thread = t;
}
static inline uint32_t arch_cycle_count(void) {
static inline ulong arch_cycle_count(void) {
#if RISCV_M_MODE
// use M version of the cycle if we're in machine mode. Some
// cpus dont have a U mode alias for this.

View File

@@ -42,14 +42,17 @@ static inline bool arch_ints_disabled(void) {
return !(state & (1<<9));
}
static inline uint32_t arch_cycle_count(void) {
#if !X86_LEGACY
static inline ulong arch_cycle_count(void) {
#if X86_LEGACY
return 0;
#elif ARCH_X86_64
uint32_t low, high;
rdtsc(low, high);
return ((ulong)high << 32) | low;
#else
uint32_t timestamp;
rdtscl(timestamp);
return timestamp;
#else
return 0;
#endif
}

View File

@@ -206,9 +206,6 @@ static inline void x86_restore_flags(uint32_t flags) {
#define rdtscl(low) \
__asm__ __volatile__("rdtsc" : "=a" (low) : : "edx")
#define rdtscll(val) \
__asm__ __volatile__("rdtsc" : "=A" (val))
static inline uint8_t inp(uint16_t _port) {
uint8_t rv;
__asm__ __volatile__ ("inb %1, %0"