Skip to content

Commit ddf70f1

Browse files
authored
Merge pull request #6377 from scartmell-arm/feature-deep-sleep-tracing-filename-fix
Replace runtime strip_path function with compiler intrinsic equivalents
2 parents 26e1275 + ab2abcb commit ddf70f1

File tree

3 files changed

+37
-57
lines changed

3 files changed

+37
-57
lines changed

hal/mbed_sleep_manager.c

Lines changed: 12 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -30,44 +30,20 @@ static uint16_t deep_sleep_lock = 0U;
3030

3131
#ifdef MBED_SLEEP_TRACING_ENABLED
3232

33-
// Length of the identifier extracted from the driver name to store for logging.
34-
#define IDENTIFIER_WIDTH 15
3533
// Number of drivers that can be stored in the structure
3634
#define STATISTIC_COUNT 10
3735

3836
typedef struct sleep_statistic {
39-
char identifier[IDENTIFIER_WIDTH];
37+
const char* identifier;
4038
uint8_t count;
4139
} sleep_statistic_t;
4240

4341
static sleep_statistic_t sleep_stats[STATISTIC_COUNT];
4442

45-
static const char* strip_path(const char* const filename)
46-
{
47-
char *output = strrchr(filename, '/');
48-
49-
if (output != NULL) {
50-
return output + 1;
51-
}
52-
53-
output = strrchr(filename, '\\');
54-
55-
if (output != NULL) {
56-
return output + 1;
57-
}
58-
59-
return filename;
60-
}
61-
6243
static sleep_statistic_t* sleep_tracker_find(const char *const filename)
6344
{
64-
char temp[IDENTIFIER_WIDTH];
65-
strncpy(temp, filename, IDENTIFIER_WIDTH);
66-
temp[IDENTIFIER_WIDTH - 1] = '\0';
67-
68-
// Search for the a driver matching the current name and return it's index
6945
for (int i = 0; i < STATISTIC_COUNT; ++i) {
70-
if (strcmp(sleep_stats[i].identifier, temp) == 0) {
46+
if (sleep_stats[i].identifier == filename) {
7147
return &sleep_stats[i];
7248
}
7349
}
@@ -77,15 +53,9 @@ static sleep_statistic_t* sleep_tracker_find(const char *const filename)
7753

7854
static sleep_statistic_t* sleep_tracker_add(const char* const filename)
7955
{
80-
char temp[IDENTIFIER_WIDTH];
81-
strncpy(temp, filename, IDENTIFIER_WIDTH);
82-
temp[IDENTIFIER_WIDTH - 1] = '\0';
83-
8456
for (int i = 0; i < STATISTIC_COUNT; ++i) {
85-
if (sleep_stats[i].identifier[0] == '\0') {
86-
core_util_critical_section_enter();
87-
strncpy(sleep_stats[i].identifier, temp, sizeof(temp));
88-
core_util_critical_section_exit();
57+
if (sleep_stats[i].identifier == NULL) {
58+
sleep_stats[i].identifier = filename;
8959

9060
return &sleep_stats[i];
9161
}
@@ -104,45 +74,42 @@ static void sleep_tracker_print_stats(void)
10474
continue;
10575
}
10676

107-
if (sleep_stats[i].identifier[0] == '\0') {
77+
if (sleep_stats[i].identifier == NULL) {
10878
return;
10979
}
11080

11181
debug("[id: %s, count: %u]\r\n", sleep_stats[i].identifier,
112-
sleep_stats[i].count);
82+
sleep_stats[i].count);
11383
}
11484
}
11585

11686
void sleep_tracker_lock(const char* const filename, int line)
11787
{
118-
const char* const stripped_path = strip_path(filename);
119-
120-
sleep_statistic_t* stat = sleep_tracker_find(stripped_path);
88+
sleep_statistic_t *stat = sleep_tracker_find(filename);
12189

12290
// Entry for this driver does not exist, create one.
12391
if (stat == NULL) {
124-
stat = sleep_tracker_add(stripped_path);
92+
stat = sleep_tracker_add(filename);
12593
}
12694

12795
core_util_atomic_incr_u8(&stat->count, 1);
12896

129-
debug("LOCK: %s, ln: %i, lock count: %u\r\n", stripped_path, line, deep_sleep_lock);
97+
debug("LOCK: %s, ln: %i, lock count: %u\r\n", filename, line, deep_sleep_lock);
13098
}
13199

132100
void sleep_tracker_unlock(const char* const filename, int line)
133101
{
134-
const char* const stripped_path = strip_path(filename);
135-
sleep_statistic_t* stat = sleep_tracker_find(stripped_path);
102+
sleep_statistic_t *stat = sleep_tracker_find(filename);
136103

137104
// Entry for this driver does not exist, something went wrong.
138105
if (stat == NULL) {
139-
debug("Unlocking sleep for driver that was not previously locked: %s, ln: %i\r\n", stripped_path, line);
106+
debug("Unlocking sleep for driver that was not previously locked: %s, ln: %i\r\n", filename, line);
140107
return;
141108
}
142109

143110
core_util_atomic_decr_u8(&stat->count, 1);
144111

145-
debug("UNLOCK: %s, ln: %i, lock count: %u\r\n", stripped_path, line, deep_sleep_lock);
112+
debug("UNLOCK: %s, ln: %i, lock count: %u\r\n", filename, line, deep_sleep_lock);
146113
}
147114

148115
#endif // MBED_SLEEP_TRACING_ENABLED

platform/mbed_power_mgmt.h

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -68,18 +68,18 @@ extern "C" {
6868
void sleep_tracker_lock(const char *const filename, int line);
6969
void sleep_tracker_unlock(const char *const filename, int line);
7070

71-
#define sleep_manager_lock_deep_sleep() \
72-
do \
73-
{ \
74-
sleep_manager_lock_deep_sleep_internal(); \
75-
sleep_tracker_lock(__FILE__, __LINE__); \
71+
#define sleep_manager_lock_deep_sleep() \
72+
do \
73+
{ \
74+
sleep_manager_lock_deep_sleep_internal(); \
75+
sleep_tracker_lock(MBED_FILENAME, __LINE__); \
7676
} while (0);
7777

78-
#define sleep_manager_unlock_deep_sleep() \
79-
do \
80-
{ \
81-
sleep_manager_unlock_deep_sleep_internal(); \
82-
sleep_tracker_unlock(__FILE__, __LINE__); \
78+
#define sleep_manager_unlock_deep_sleep() \
79+
do \
80+
{ \
81+
sleep_manager_unlock_deep_sleep_internal(); \
82+
sleep_tracker_unlock(MBED_FILENAME, __LINE__); \
8383
} while (0);
8484

8585
#else
@@ -121,7 +121,6 @@ void sleep_manager_unlock_deep_sleep_internal(void);
121121
*/
122122
bool sleep_manager_can_deep_sleep(void);
123123

124-
125124
/** Enter auto selected sleep mode. It chooses the sleep or deeepsleep modes based
126125
* on the deepsleep locking counter
127126
*

platform/mbed_toolchain.h

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -376,13 +376,27 @@
376376
#endif
377377
#endif
378378

379+
// Macro containing the filename part of the value of __FILE__. Defined as
380+
// string literal.
381+
#ifndef MBED_FILENAME
382+
#if defined(__CC_ARM)
383+
#define MBED_FILENAME __MODULE__
384+
#elif defined(__GNUC__)
385+
#define MBED_FILENAME (__builtin_strrchr(__FILE__, '/') ? __builtin_strrchr(__FILE__, '/') + 1 : __builtin_strrchr(__FILE__, '\\') ? __builtin_strrchr(__FILE__, '\\') + 1 : __FILE__)
386+
#elif defined(__ICCARM__)
387+
#define MBED_FILENAME (strrchr(__FILE__, '/') ? strrchr(__FILE__, '/') + 1 : strrchr(__FILE__, '\\') ? strrchr(__FILE__, '\\') + 1 : __FILE__)
388+
#else
389+
#define MBED_FILENAME __FILE__
390+
#endif
391+
#endif // #ifndef MBED_FILENAME
392+
379393
// FILEHANDLE declaration
380394
#if defined(TOOLCHAIN_ARM)
381395
#include <rt_sys.h>
382396
#endif
383397

384398
#ifndef FILEHANDLE
385-
typedef int FILEHANDLE;
399+
typedef int FILEHANDLE;
386400
#endif
387401

388402
// Backwards compatibility

0 commit comments

Comments
 (0)