mirror of
https://github.com/apache/nuttx-apps.git
synced 2026-08-03 21:29:05 +00:00
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>
146 lines
4.2 KiB
C
146 lines
4.2 KiB
C
/****************************************************************************
|
|
* apps/testing/sched/pthread_mutex_perf/pthread_mutex_perf.c
|
|
*
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
*
|
|
* Licensed to the Apache Software Foundation (ASF) under one or more
|
|
* contributor license agreements. See the NOTICE file distributed with
|
|
* this work for additional information regarding copyright ownership. The
|
|
* ASF licenses this file to you under the Apache License, Version 2.0 (the
|
|
* "License"); you may not use this file except in compliance with the
|
|
* License. You may obtain a copy of the License at
|
|
*
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
*
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
|
* License for the specific language governing permissions and limitations
|
|
* under the License.
|
|
*
|
|
****************************************************************************/
|
|
|
|
/****************************************************************************
|
|
* Included Files
|
|
****************************************************************************/
|
|
|
|
#include <nuttx/config.h>
|
|
#include <stdint.h>
|
|
#include <stdio.h>
|
|
#include <pthread.h>
|
|
|
|
/****************************************************************************
|
|
* Private Functions
|
|
****************************************************************************/
|
|
|
|
pthread_mutex_t g_mutex = PTHREAD_MUTEX_INITIALIZER;
|
|
|
|
static void timespec_diff(const struct timespec *start,
|
|
const struct timespec *end,
|
|
struct timespec *diff)
|
|
{
|
|
diff->tv_sec = end->tv_sec - start->tv_sec;
|
|
diff->tv_nsec = end->tv_nsec - start->tv_nsec;
|
|
|
|
if (diff->tv_nsec < 0)
|
|
{
|
|
diff->tv_sec--;
|
|
diff->tv_nsec += 1000000000;
|
|
}
|
|
}
|
|
|
|
static void timespec_add(struct timespec *total, const struct timespec *diff)
|
|
{
|
|
total->tv_sec += diff->tv_sec;
|
|
total->tv_nsec += diff->tv_nsec;
|
|
|
|
if (total->tv_nsec >= 1000000000)
|
|
{
|
|
total->tv_sec += total->tv_nsec / 1000000000;
|
|
total->tv_nsec = total->tv_nsec % 1000000000;
|
|
}
|
|
}
|
|
|
|
static void timespec_avg(const struct timespec *total, int count,
|
|
struct timespec *avg)
|
|
{
|
|
uint64_t total_ns = total->tv_sec * 1000000000 + total->tv_nsec;
|
|
uint64_t avg_ns = total_ns / count;
|
|
|
|
avg->tv_sec = avg_ns / 1000000000;
|
|
avg->tv_nsec = avg_ns % 1000000000;
|
|
}
|
|
|
|
/****************************************************************************
|
|
* Public Functions
|
|
****************************************************************************/
|
|
|
|
/****************************************************************************
|
|
* pmp_main
|
|
****************************************************************************/
|
|
|
|
int main(int argc, char *argv[])
|
|
{
|
|
struct timespec start;
|
|
struct timespec end;
|
|
struct timespec diff;
|
|
struct timespec total = {
|
|
0, 0
|
|
};
|
|
|
|
struct timespec avg;
|
|
int i;
|
|
int j = 0;
|
|
const int loop_count = 10;
|
|
|
|
while (j < loop_count)
|
|
{
|
|
i = 0;
|
|
j++;
|
|
|
|
/* Get the starting time */
|
|
|
|
clock_gettime(CLOCK_BOOTTIME, &start);
|
|
|
|
/* Do 1 million interactions trying to lock an already locked mutex */
|
|
|
|
pthread_mutex_lock(&g_mutex);
|
|
|
|
while (i < 1000 * 1000)
|
|
{
|
|
i++;
|
|
pthread_mutex_trylock(&g_mutex);
|
|
}
|
|
|
|
pthread_mutex_unlock(&g_mutex);
|
|
|
|
/* Get the finished time */
|
|
|
|
clock_gettime(CLOCK_BOOTTIME, &end);
|
|
|
|
/* Get the calculated elapsed time */
|
|
|
|
timespec_diff(&start, &end, &diff);
|
|
|
|
/* Add it to total time for each loop pass */
|
|
|
|
timespec_add(&total, &diff);
|
|
|
|
/* Get the average time */
|
|
|
|
timespec_avg(&total, j, &avg);
|
|
|
|
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: %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;
|
|
}
|