mirror of
https://github.com/apache/nuttx-apps.git
synced 2026-08-01 20:29:00 +00:00
!apps: drop redundant casts on tv_sec/tv_nsec and fix printf formats
Now that time_t is unconditionally 64-bit (signed int64_t) and the
struct timespec fields tv_sec / tv_nsec are wide enough on their own,
the explicit (uint64_t)/(int64_t)/(int) casts that used to guard the
multiplications and subtractions in *_us / *_ms / *_ns helpers are no
longer needed. Drop them to keep the timekeeping math readable.
In the same spirit, this commit also normalises the printf-style format
specifiers and casts used to print tv_sec / tv_nsec / tv_usec values.
The prior code was a mix of "%d"/"%u"/"%ld"/"%lu"/"%lld" with matching
(int)/(unsigned long)/(long long) casts; some formats truncated time_t
on 32-bit hosts, others mismatched signedness or width. Replace all
such cases with the portable POSIX-recommended forms:
- tv_sec (time_t, signed, impl-defined width) -> %jd + (intmax_t)
- tv_nsec (long, signed) -> %ld (no cast)
- tv_usec (suseconds_t / long) -> %ld (no cast)
Also drop two stale `(FAR const time_t *)&ts.tv_sec` casts that are
unnecessary now that ts.tv_sec is plain time_t.
Arithmetic-cleanup files (existing scope):
- benchmarks/cyclictest/cyclictest.c: timediff_us()
- benchmarks/sd_bench/sd_bench_main.c: get_time_delta_us()
- examples/oneshot/oneshot_main.c: maxus computation
- examples/watchdog/watchdog_main.c: current_time_ms (x2)
- industry/nxmodbus/nxmb_internal.h: nxmb_util_clock_ms()
- netutils/ntpclient/ntpclient.c: timespec2ntp()
- netutils/ptpd/ptpd.c: ptp_adjtime()
- system/dd/dd_main.c: elapsed accounting
- testing/drivers/drivertest/drivertest_posix_timer.c:
get_timestamp()
- testing/drivers/sd_stress/sd_stress_main.c:get_time_delta()
- testing/sched/getprime/getprime_main.c: elapsed accounting
- testing/sched/pthread_mutex_perf/pthread_mutex_perf.c:
timespec_avg()
Printf-format-fix files (new in this revision):
- examples/adjtime/adjtime_main.c
- examples/charger/charger_main.c
- examples/netpkt/netpkt_ethercat.c
- fsutils/mkfatfs/mkfatfs.c
- graphics/tiff/tiff_initialize.c
- netutils/ptpd/ptpd.c
- nshlib/nsh_timcmds.c
- system/coredump/coredump.c
- system/ptpd/ptpd_main.c
- testing/drivers/drivertest/drivertest_oneshot.c
- testing/mm/kasantest/kasantest.c
- testing/ostest/semtimed.c
- testing/sched/pthread_mutex_perf/pthread_mutex_perf.c
- testing/sched/timerjitter/timerjitter.c
- testing/testsuites/kernel/time/cases/clock_test_clock01.c
- testing/testsuites/kernel/time/cases/clock_test_smoke.c
No behavioural change.
Signed-off-by: Xiang Xiao <xiaoxiang@xiaomi.com>
This commit is contained in:
parent
e9d1141f7c
commit
f9f59bd0f8
32 changed files with 144 additions and 139 deletions
|
|
@ -167,7 +167,7 @@ static void drivertest_oneshot(FAR void **state)
|
|||
ret = ioctl(fd, OSIOC_MAXDELAY, &ts);
|
||||
assert_return_code(ret, OK);
|
||||
|
||||
syslog(LOG_DEBUG, "maxdelay sec:%lld\n", (long long)ts.tv_sec);
|
||||
syslog(LOG_DEBUG, "maxdelay sec:%jd\n", (intmax_t)ts.tv_sec);
|
||||
syslog(LOG_DEBUG, "maxdelay nsec:%ld\n", ts.tv_nsec);
|
||||
|
||||
for (i = 0; i < ONESHOT_DEFAULT_NSAMPLES; i++)
|
||||
|
|
|
|||
|
|
@ -146,7 +146,7 @@ static uint64_t get_timestamp(void)
|
|||
struct timespec ts;
|
||||
uint64_t ms;
|
||||
clock_gettime(CLOCK_MONOTONIC, &ts);
|
||||
ms = (uint64_t)ts.tv_sec * 1000 + ts.tv_nsec / 1000000;
|
||||
ms = ts.tv_sec * 1000 + ts.tv_nsec / 1000000;
|
||||
return ms;
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -134,7 +134,7 @@ static bool create_files(const char *dir, const char *name,
|
|||
|
||||
if (!read_bytes)
|
||||
{
|
||||
printf("malloc failed for read bytes bufffer\n");
|
||||
printf("malloc failed for read bytes buffer\n");
|
||||
return false;
|
||||
}
|
||||
|
||||
|
|
@ -282,8 +282,8 @@ static uint64_t get_time_delta(const struct timespec *start,
|
|||
const struct timespec *end)
|
||||
{
|
||||
uint64_t elapsed;
|
||||
elapsed = (((uint64_t)end->tv_sec * NSEC_PER_SEC) + end->tv_nsec);
|
||||
elapsed -= (((uint64_t)start->tv_sec * NSEC_PER_SEC) + start->tv_nsec);
|
||||
elapsed = (end->tv_sec * NSEC_PER_SEC) + end->tv_nsec;
|
||||
elapsed -= (start->tv_sec * NSEC_PER_SEC) + start->tv_nsec;
|
||||
return elapsed / 1000;
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -666,9 +666,9 @@ static int run_testcase(int argc, FAR char *argv[])
|
|||
clock_gettime(CLOCK_MONOTONIC, &end);
|
||||
|
||||
timespec_sub(&result, &end, &start);
|
||||
printf("%s spending %ld.%lds\n", run->testcase->name,
|
||||
result.tv_sec,
|
||||
result.tv_nsec);
|
||||
printf("%s spending %jd.%09lds\n", run->testcase->name,
|
||||
(intmax_t)result.tv_sec,
|
||||
result.tv_nsec);
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -29,6 +29,7 @@
|
|||
#include <pthread.h>
|
||||
#include <sched.h>
|
||||
#include <semaphore.h>
|
||||
#include <stdint.h>
|
||||
#include <stdio.h>
|
||||
#include <time.h>
|
||||
#include <unistd.h>
|
||||
|
|
@ -150,10 +151,10 @@ void semtimed_test(void)
|
|||
}
|
||||
}
|
||||
|
||||
printf("BEFORE: (%lu sec, %lu nsec)\n",
|
||||
(unsigned long)before.tv_sec, (unsigned long)before.tv_nsec);
|
||||
printf("AFTER: (%lu sec, %lu nsec)\n",
|
||||
(unsigned long)after.tv_sec, (unsigned long)after.tv_nsec);
|
||||
printf("BEFORE: (%jd sec, %ld nsec)\n",
|
||||
(intmax_t)before.tv_sec, before.tv_nsec);
|
||||
printf("AFTER: (%jd sec, %ld nsec)\n",
|
||||
(intmax_t)after.tv_sec, after.tv_nsec);
|
||||
|
||||
/* Now make sure that the time wait returns successfully if the semaphore
|
||||
* is posted
|
||||
|
|
@ -246,10 +247,10 @@ void semtimed_test(void)
|
|||
printf("semtimed_test: PASS: sem_timedwait succeeded\n");
|
||||
}
|
||||
|
||||
printf("BEFORE: (%lu sec, %lu nsec)\n",
|
||||
(unsigned long)before.tv_sec, (unsigned long)before.tv_nsec);
|
||||
printf("AFTER: (%lu sec, %lu nsec)\n",
|
||||
(unsigned long)after.tv_sec, (unsigned long)after.tv_nsec);
|
||||
printf("BEFORE: (%jd sec, %ld nsec)\n",
|
||||
(intmax_t)before.tv_sec, before.tv_nsec);
|
||||
printf("AFTER: (%jd sec, %ld nsec)\n",
|
||||
(intmax_t)after.tv_sec, after.tv_nsec);
|
||||
|
||||
/* Clean up detritus left by the pthread */
|
||||
|
||||
|
|
|
|||
|
|
@ -217,8 +217,8 @@ int main(int argc, FAR char *argv[])
|
|||
get_prime_in_parallel(n);
|
||||
clock_gettime(CLOCK_REALTIME, &ts1);
|
||||
|
||||
elapsed = (((uint64_t)ts1.tv_sec * NSEC_PER_SEC) + ts1.tv_nsec);
|
||||
elapsed -= (((uint64_t)ts0.tv_sec * NSEC_PER_SEC) + ts0.tv_nsec);
|
||||
elapsed = (ts1.tv_sec * NSEC_PER_SEC) + ts1.tv_nsec;
|
||||
elapsed -= (ts0.tv_sec * NSEC_PER_SEC) + ts0.tv_nsec;
|
||||
elapsed /= NSEC_PER_MSEC; /* msec */
|
||||
|
||||
printf("%s took %" PRIu64 " msec\n", argv[0], elapsed);
|
||||
|
|
|
|||
|
|
@ -25,6 +25,7 @@
|
|||
****************************************************************************/
|
||||
|
||||
#include <nuttx/config.h>
|
||||
#include <stdint.h>
|
||||
#include <stdio.h>
|
||||
#include <pthread.h>
|
||||
|
||||
|
|
@ -63,7 +64,7 @@ static void timespec_add(struct timespec *total, const struct timespec *diff)
|
|||
static void timespec_avg(const struct timespec *total, int count,
|
||||
struct timespec *avg)
|
||||
{
|
||||
uint64_t total_ns = (uint64_t)total->tv_sec * 1000000000 + total->tv_nsec;
|
||||
uint64_t total_ns = total->tv_sec * 1000000000 + total->tv_nsec;
|
||||
uint64_t avg_ns = total_ns / count;
|
||||
|
||||
avg->tv_sec = avg_ns / 1000000000;
|
||||
|
|
@ -129,17 +130,17 @@ int main(int argc, char *argv[])
|
|||
|
||||
timespec_avg(&total, j, &avg);
|
||||
|
||||
printf("%d: diff = %lu.%09lu s | avg = %lu.%09lu s\n", j,
|
||||
(unsigned long)diff.tv_sec, (unsigned long)diff.tv_nsec,
|
||||
(unsigned long)avg.tv_sec, (unsigned long)avg.tv_nsec);
|
||||
printf("%d: diff = %jd.%09ld s | avg = %jd.%09ld s\n", j,
|
||||
(intmax_t)diff.tv_sec, diff.tv_nsec,
|
||||
(intmax_t)avg.tv_sec, avg.tv_nsec);
|
||||
}
|
||||
|
||||
printf("\n===== result =====\n");
|
||||
printf("count: %d\n", loop_count);
|
||||
printf("total: %lu.%09lu s\n", (unsigned long)total.tv_sec,
|
||||
(unsigned long)total.tv_nsec);
|
||||
printf("avg: %lu.%09lu s\n", (unsigned long)avg.tv_sec,
|
||||
(unsigned long)avg.tv_nsec);
|
||||
printf("total: %jd.%09ld s\n", (intmax_t)total.tv_sec,
|
||||
total.tv_nsec);
|
||||
printf("avg: %jd.%09ld s\n", (intmax_t)avg.tv_sec,
|
||||
avg.tv_nsec);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -25,6 +25,7 @@
|
|||
****************************************************************************/
|
||||
|
||||
#include <pthread.h>
|
||||
#include <stdint.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <signal.h>
|
||||
|
|
@ -213,8 +214,8 @@ static FAR void *timerjitter(FAR void *arg)
|
|||
diff = calc_diff(&now, &next);
|
||||
if (param->print)
|
||||
{
|
||||
printf("diff %"PRId64", now %"PRId64".%09lu\n", diff, now.tv_sec,
|
||||
now.tv_nsec);
|
||||
printf("diff %"PRId64", now %jd.%09ld\n", diff,
|
||||
(intmax_t)now.tv_sec, now.tv_nsec);
|
||||
}
|
||||
|
||||
if (diff > param->max)
|
||||
|
|
|
|||
|
|
@ -224,8 +224,8 @@ void pread01_l_seek(int fdesc, off_t offset, int whence, off_t checkoff)
|
|||
if ((offloc = lseek(fdesc, offset, whence)) != checkoff)
|
||||
{
|
||||
syslog(LOG_WARNING,
|
||||
"return = %" PRId64 " , expected %" PRId64 "\n",
|
||||
(int64_t)offloc, (int64_t)checkoff);
|
||||
"return = %jd, expected %jd\n",
|
||||
(intmax_t)offloc, (intmax_t)checkoff);
|
||||
syslog(LOG_ERR, "lseek() on %s failed\n", pread01_filename);
|
||||
}
|
||||
}
|
||||
|
|
@ -234,7 +234,7 @@ void pread01_l_seek(int fdesc, off_t offset, int whence, off_t checkoff)
|
|||
* Name: pread01_compare_bufers
|
||||
****************************************************************************/
|
||||
|
||||
/* compare_bufers() - Compare the contents of read buffer aganist the
|
||||
/* compare_bufers() - Compare the contents of read buffer against the
|
||||
* write buffer contents.
|
||||
*
|
||||
* The contents of the index of each buffer should be as follows:
|
||||
|
|
@ -259,7 +259,7 @@ int pread01_compare_bufers(void)
|
|||
}
|
||||
}
|
||||
|
||||
/* If no erros, Test successful */
|
||||
/* If no errors, Test successful */
|
||||
|
||||
if (!err_flg)
|
||||
{
|
||||
|
|
|
|||
|
|
@ -140,8 +140,8 @@ void test_nuttx_syscall_truncate01(FAR void **state)
|
|||
{
|
||||
syslog(LOG_ERR,
|
||||
"FAIL, %s: Incorrect file "
|
||||
"size %" PRId64 " , Expected %d\n",
|
||||
truncate01_fileneme, (int64_t)file_length,
|
||||
"size %jd, Expected %d\n",
|
||||
truncate01_fileneme, (intmax_t)file_length,
|
||||
TRUNC_LEN);
|
||||
fail_msg("test fail !");
|
||||
}
|
||||
|
|
|
|||
|
|
@ -79,8 +79,8 @@ void test_nuttx_syscall_write01(FAR void **state)
|
|||
{
|
||||
badcount++;
|
||||
syslog(LOG_INFO,
|
||||
"INFO, write() returned %" PRId64 ", expected %d\n",
|
||||
(int64_t)ret, i);
|
||||
"INFO, write() returned %zd, expected %d\n",
|
||||
ret, i);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -160,8 +160,8 @@ void test_nuttx_syscall_write03(FAR void **state)
|
|||
{
|
||||
badcount++;
|
||||
syslog(LOG_INFO,
|
||||
"INFO, write() returned %" PRId64 ", expected %d\n",
|
||||
(int64_t)ret, i);
|
||||
"INFO, write() returned %zd, expected %d\n",
|
||||
ret, i);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -61,8 +61,8 @@ void test_nuttx_clock_test_clock01(FAR void **state)
|
|||
/* get current real time */
|
||||
|
||||
ret = clock_gettime(clk, &oldtp);
|
||||
syslog(LOG_INFO, "The current real time: sec is %lld, nsec is %ld\n",
|
||||
(long long)oldtp.tv_sec, oldtp.tv_nsec);
|
||||
syslog(LOG_INFO, "The current real time: sec is %jd, nsec is %ld\n",
|
||||
(intmax_t)oldtp.tv_sec, oldtp.tv_nsec);
|
||||
assert_int_equal(ret, 0);
|
||||
|
||||
syslog(LOG_INFO, "sleep 2 seconds\n");
|
||||
|
|
@ -76,8 +76,8 @@ void test_nuttx_clock_test_clock01(FAR void **state)
|
|||
/* set real time */
|
||||
|
||||
ret = clock_settime(clk, &tp);
|
||||
syslog(LOG_INFO, "Setting time: sec is %lld, nsec is %ld\n",
|
||||
(long long)tp.tv_sec, tp.tv_nsec);
|
||||
syslog(LOG_INFO, "Setting time: sec is %jd, nsec is %ld\n",
|
||||
(intmax_t)tp.tv_sec, tp.tv_nsec);
|
||||
assert_int_equal(ret, 0);
|
||||
|
||||
syslog(LOG_INFO, "get real time clock again\n");
|
||||
|
|
@ -86,9 +86,9 @@ void test_nuttx_clock_test_clock01(FAR void **state)
|
|||
|
||||
ret = clock_gettime(clk, &tp);
|
||||
syslog(LOG_INFO,
|
||||
"Obtaining the current time after setting: sec = %lld, nsec = "
|
||||
"Obtaining the current time after setting: sec = %jd, nsec = "
|
||||
"%ld\n",
|
||||
(long long)tp.tv_sec, tp.tv_nsec);
|
||||
(intmax_t)tp.tv_sec, tp.tv_nsec);
|
||||
passflag = (tp.tv_sec >= 2 + oldtp.tv_sec) &&
|
||||
(tp.tv_sec <=
|
||||
2 + oldtp.tv_sec + 1); /* 2, use for testing clock setting */
|
||||
|
|
|
|||
|
|
@ -52,7 +52,7 @@ void test_nuttx_clock_test_smoke01(FAR void **state)
|
|||
0, 0
|
||||
};
|
||||
|
||||
struct timespec setts =
|
||||
struct timespec tp =
|
||||
{
|
||||
0, 0
|
||||
};
|
||||
|
|
@ -79,30 +79,30 @@ void test_nuttx_clock_test_smoke01(FAR void **state)
|
|||
|
||||
ret = clock_gettime(clk, &oldtp);
|
||||
syslog(LOG_INFO,
|
||||
"the clock current time: %lld second, %ld nanosecond\n",
|
||||
(long long)oldtp.tv_sec, oldtp.tv_nsec);
|
||||
"the clock current time: %jd second, %ld nanosecond\n",
|
||||
(intmax_t)oldtp.tv_sec, oldtp.tv_nsec);
|
||||
assert_int_equal(ret, 0);
|
||||
|
||||
/* set clock realtime */
|
||||
|
||||
setts.tv_sec = oldtp.tv_sec + 1;
|
||||
setts.tv_nsec = oldtp.tv_nsec;
|
||||
tp.tv_sec = oldtp.tv_sec + 1;
|
||||
tp.tv_nsec = oldtp.tv_nsec;
|
||||
syslog(LOG_INFO,
|
||||
"the clock setting time: %lld second, %ld nanosecond\n",
|
||||
(long long)setts.tv_sec, setts.tv_nsec);
|
||||
ret = clock_settime(CLOCK_REALTIME, &setts);
|
||||
"the clock setting time: %jd second, %ld nanosecond\n",
|
||||
(intmax_t)tp.tv_sec, tp.tv_nsec);
|
||||
ret = clock_settime(CLOCK_REALTIME, &tp);
|
||||
assert_int_equal(ret, 0);
|
||||
|
||||
ret = clock_gettime(clk, &ts);
|
||||
syslog(LOG_INFO,
|
||||
"obtaining the current time after setting: %lld second, %ld "
|
||||
"obtaining the current time after setting: %jd second, %ld "
|
||||
"nanosecond\n",
|
||||
(long long)ts.tv_sec, ts.tv_nsec);
|
||||
(intmax_t)ts.tv_sec, ts.tv_nsec);
|
||||
|
||||
passflag =
|
||||
(ts.tv_sec >= setts.tv_sec) &&
|
||||
(ts.tv_sec >= tp.tv_sec) &&
|
||||
(ts.tv_sec <=
|
||||
setts.tv_sec + 1); /* 1, means obtaining time's errno is 1 second. */
|
||||
tp.tv_sec + 1); /* 1, means obtaining time's errno is 1 second. */
|
||||
assert_int_equal(ret, 0);
|
||||
assert_int_equal(passflag, 1);
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue