Skip to content

Commit 8cb7dd0

Browse files
committed
apps/testing: Add pthread_mutex_perf created by Anchao
During the discussion about the impact of adding counter to TCB Mr anchao created a pthread mutex performance example: apache/nuttx#17468 (comment) Because this example can exercise (not ideally) the pthread mutex I decided to add it to apps/testing/sched, the program name will be called "pmutexp" (because "pmp" is not a good name). Signed-off-by: Alan C. Assis <[email protected]>
1 parent a5e455c commit 8cb7dd0

File tree

5 files changed

+273
-0
lines changed

5 files changed

+273
-0
lines changed
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
# ##############################################################################
2+
# apps/testing/sched/pthread_mutex_perf/CMakeLists.txt
3+
#
4+
# SPDX-License-Identifier: Apache-2.0
5+
#
6+
# Licensed to the Apache Software Foundation (ASF) under one or more contributor
7+
# license agreements. See the NOTICE file distributed with this work for
8+
# additional information regarding copyright ownership. The ASF licenses this
9+
# file to you under the Apache License, Version 2.0 (the "License"); you may not
10+
# use this file except in compliance with the License. You may obtain a copy of
11+
# the License at
12+
#
13+
# http://www.apache.org/licenses/LICENSE-2.0
14+
#
15+
# Unless required by applicable law or agreed to in writing, software
16+
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
17+
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
18+
# License for the specific language governing permissions and limitations under
19+
# the License.
20+
#
21+
# ##############################################################################
22+
23+
if(CONFIG_TESTING_PTHREAD_MUTEX_PERF)
24+
25+
set(SRCS pthread_mutex_perf.c)
26+
27+
nuttx_add_application(
28+
NAME
29+
${CONFIG_TESTING_PTHREAD_MUTEX_PERF_PROGNMAE}
30+
PRIORITY
31+
${CONFIG_TESTING_PTHREAD_MUTEX_PERF_PRIORITY}
32+
STACKSIZE
33+
${CONFIG_TESTING_PTHREAD_MUTEX_PERF_STACKSIZE}
34+
MODULE
35+
${CONFIG_TESTING_PTHREAD_MUTEX_PERF}
36+
COMPILE_FLAGS
37+
${FLAGS}
38+
SRCS
39+
${SRCS})
40+
41+
endif()
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#
2+
# For a description of the syntax of this configuration file,
3+
# see the file kconfig-language.txt in the NuttX tools repository.
4+
#
5+
6+
config TESTING_PTHREAD_MUTEX_PERF
7+
bool "Pthread Mutex Performance (pmutexp) testing"
8+
default n
9+
---help---
10+
Pthread Mutex Performance (pmp) helps to analyze the pthread
11+
mutex performance, by calling the function many times.
12+
13+
if TESTING_PTHREAD_MUTEX_PERF
14+
15+
config TESTING_PTHREAD_MUTEX_PERF_PROGNAME
16+
string "Program name"
17+
default "pmutexp"
18+
---help---
19+
This is the name of the program that will be used when the NSH ELF
20+
program is installed.
21+
22+
config TESTING_PTHREAD_MUTEX_PERF_PRIORITY
23+
int "Priority of pmutexp process"
24+
default 100
25+
26+
config TESTING_PTHREAD_MUTEX_PERF_STACKSIZE
27+
int "Stack size of pmutexp process"
28+
default DEFAULT_TASK_STACKSIZE
29+
30+
endif
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
############################################################################
2+
# apps/testing/sched/pthread_mutex_perf/Make.defs
3+
#
4+
# SPDX-License-Identifier: Apache-2.0
5+
#
6+
# Licensed to the Apache Software Foundation (ASF) under one or more
7+
# contributor license agreements. See the NOTICE file distributed with
8+
# this work for additional information regarding copyright ownership. The
9+
# ASF licenses this file to you under the Apache License, Version 2.0 (the
10+
# "License"); you may not use this file except in compliance with the
11+
# License. You may obtain a copy of the License at
12+
#
13+
# http://www.apache.org/licenses/LICENSE-2.0
14+
#
15+
# Unless required by applicable law or agreed to in writing, software
16+
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
17+
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
18+
# License for the specific language governing permissions and limitations
19+
# under the License.
20+
#
21+
############################################################################
22+
23+
ifneq ($(CONFIG_TESTING_PTHREAD_MUTEX_PERF),)
24+
CONFIGURED_APPS += $(APPDIR)/testing/sched/pthread_mutex_perf
25+
endif
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
############################################################################
2+
# apps/testing/sched/pthread_mutex_perf/Makefile
3+
#
4+
# SPDX-License-Identifier: Apache-2.0
5+
#
6+
# Licensed to the Apache Software Foundation (ASF) under one or more
7+
# contributor license agreements. See the NOTICE file distributed with
8+
# this work for additional information regarding copyright ownership. The
9+
# ASF licenses this file to you under the Apache License, Version 2.0 (the
10+
# "License"); you may not use this file except in compliance with the
11+
# License. You may obtain a copy of the License at
12+
#
13+
# http://www.apache.org/licenses/LICENSE-2.0
14+
#
15+
# Unless required by applicable law or agreed to in writing, software
16+
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
17+
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
18+
# License for the specific language governing permissions and limitations
19+
# under the License.
20+
#
21+
############################################################################
22+
23+
include $(APPDIR)/Make.defs
24+
25+
PROGNAME = $(CONFIG_TESTING_PTHREAD_MUTEX_PERF_PROGNAME)
26+
PRIORITY = $(CONFIG_TESTING_PTHREAD_MUTEX_PERF_PRIORITY)
27+
STACKSIZE = $(CONFIG_TESTING_PTHREAD_MUTEX_PERF_STACKSIZE)
28+
MODULE = $(CONFIG_TESTING_PTHREAD_MUTEX_PERF)
29+
30+
MAINSRC = pthread_mutex_perf.c
31+
32+
include $(APPDIR)/Application.mk
Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
1+
/****************************************************************************
2+
* apps/testing/sched/pthread_mutex_perf/pthread_mutex_perf.c
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*
6+
* Licensed to the Apache Software Foundation (ASF) under one or more
7+
* contributor license agreements. See the NOTICE file distributed with
8+
* this work for additional information regarding copyright ownership. The
9+
* ASF licenses this file to you under the Apache License, Version 2.0 (the
10+
* "License"); you may not use this file except in compliance with the
11+
* License. You may obtain a copy of the License at
12+
*
13+
* http://www.apache.org/licenses/LICENSE-2.0
14+
*
15+
* Unless required by applicable law or agreed to in writing, software
16+
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
17+
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
18+
* License for the specific language governing permissions and limitations
19+
* under the License.
20+
*
21+
****************************************************************************/
22+
23+
/****************************************************************************
24+
* Included Files
25+
****************************************************************************/
26+
27+
#include <nuttx/config.h>
28+
#include <stdio.h>
29+
#include <pthread.h>
30+
31+
/****************************************************************************
32+
* Private Functions
33+
****************************************************************************/
34+
35+
pthread_mutex_t g_mutex = PTHREAD_MUTEX_INITIALIZER;
36+
37+
static void timespec_diff(const struct timespec *start,
38+
const struct timespec *end,
39+
struct timespec *diff)
40+
{
41+
diff->tv_sec = end->tv_sec - start->tv_sec;
42+
diff->tv_nsec = end->tv_nsec - start->tv_nsec;
43+
44+
if (diff->tv_nsec < 0)
45+
{
46+
diff->tv_sec--;
47+
diff->tv_nsec += 1000000000;
48+
}
49+
}
50+
51+
static void timespec_add(struct timespec *total, const struct timespec *diff)
52+
{
53+
total->tv_sec += diff->tv_sec;
54+
total->tv_nsec += diff->tv_nsec;
55+
56+
if (total->tv_nsec >= 1000000000)
57+
{
58+
total->tv_sec += total->tv_nsec / 1000000000;
59+
total->tv_nsec = total->tv_nsec % 1000000000;
60+
}
61+
}
62+
63+
static void timespec_avg(const struct timespec *total, int count,
64+
struct timespec *avg)
65+
{
66+
uint64_t total_ns = (uint64_t)total->tv_sec * 1000000000 + total->tv_nsec;
67+
uint64_t avg_ns = total_ns / count;
68+
69+
avg->tv_sec = avg_ns / 1000000000;
70+
avg->tv_nsec = avg_ns % 1000000000;
71+
}
72+
73+
/****************************************************************************
74+
* Public Functions
75+
****************************************************************************/
76+
77+
/****************************************************************************
78+
* pmp_main
79+
****************************************************************************/
80+
81+
int main(int argc, char *argv[])
82+
{
83+
struct timespec start;
84+
struct timespec end;
85+
struct timespec diff;
86+
struct timespec total = {
87+
0, 0
88+
};
89+
90+
struct timespec avg;
91+
int i;
92+
int j = 0;
93+
const int loop_count = 10;
94+
95+
while (j < loop_count)
96+
{
97+
i = 0;
98+
j++;
99+
100+
/* Get the starting time */
101+
102+
clock_gettime(CLOCK_BOOTTIME, &start);
103+
104+
/* Do 1 million interactions trying to lock an already locked mutex */
105+
106+
pthread_mutex_lock(&g_mutex);
107+
108+
while (i < 1000 * 1000)
109+
{
110+
i++;
111+
pthread_mutex_trylock(&g_mutex);
112+
}
113+
114+
pthread_mutex_unlock(&g_mutex);
115+
116+
/* Get the finished time */
117+
118+
clock_gettime(CLOCK_BOOTTIME, &end);
119+
120+
/* Get the calculated elapsed time */
121+
122+
timespec_diff(&start, &end, &diff);
123+
124+
/* Add it to total time for each loop pass */
125+
126+
timespec_add(&total, &diff);
127+
128+
/* Get the average time */
129+
130+
timespec_avg(&total, j, &avg);
131+
132+
printf("%d: diff = %lu.%09lu s | avg = %lu.%09lu s\n", j,
133+
(unsigned long)diff.tv_sec, (unsigned long)diff.tv_nsec,
134+
(unsigned long)avg.tv_sec, (unsigned long)avg.tv_nsec);
135+
}
136+
137+
printf("\n===== result =====\n");
138+
printf("count: %d\n", loop_count);
139+
printf("total: %lu.%09lu s\n", (unsigned long)total.tv_sec,
140+
(unsigned long)total.tv_nsec);
141+
printf("avg: %lu.%09lu s\n", (unsigned long)avg.tv_sec,
142+
(unsigned long)avg.tv_nsec);
143+
144+
return 0;
145+
}

0 commit comments

Comments
 (0)