<commit_before>/* mbed Microcontroller Library
 * Copyright (c) 2017 ARM Limited
 *
 * Licensed 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.
 */

#include "mbed_assert.h"
#include "mbed_power_mgmt.h"
#include "mbed_critical.h"
#include "sleep_api.h"
#include "mbed_error.h"
#include "mbed_debug.h"
#include <limits.h>
#include <stdio.h>

#if DEVICE_SLEEP

// deep sleep locking counter. A target is allowed to deep sleep if counter == 0
static uint16_t deep_sleep_lock = 0U;

#ifdef MBED_SLEEP_TRACING_ENABLED

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

typedef struct sleep_statistic {
    char identifier[IDENTIFIER_WIDTH];
    uint8_t count;
} sleep_statistic_t;

static sleep_statistic_t sleep_stats[STATISTIC_COUNT];

static sleep_statistic_t* sleep_tracker_find(const char *const filename)
{
    char temp[IDENTIFIER_WIDTH];
    strncpy(temp, filename, IDENTIFIER_WIDTH);
    temp[IDENTIFIER_WIDTH - 1] = '\0';

    // Search for the a driver matching the current name and return it's index
    for (int i = 0; i < STATISTIC_COUNT; ++i) {
        if (strcmp(sleep_stats[i].identifier, temp) == 0) {
            return &sleep_stats[i];
        }
    }

    return NULL;
}

static sleep_statistic_t* sleep_tracker_add(const char* const filename)
{
    char temp[IDENTIFIER_WIDTH];
    strncpy(temp, filename, IDENTIFIER_WIDTH);
    temp[IDENTIFIER_WIDTH - 1] = '\0';

    for (int i = 0; i < STATISTIC_COUNT; ++i) {
        if (sleep_stats[i].identifier[0] == '\0') {
            core_util_critical_section_enter();
            strncpy(sleep_stats[i].identifier, temp, sizeof(temp));
            core_util_critical_section_exit();

            return &sleep_stats[i];
        }
    }

    debug("No free indexes left to use in mbed sleep tracker.\r\n");

    return NULL;
}

static void sleep_tracker_print_stats(void)
{
    debug("Sleep locks held:\r\n");
    for (int i = 0; i < STATISTIC_COUNT; ++i) {
        if (sleep_stats[i].count == 0) {
            continue;
        }

        if (sleep_stats[i].identifier[0] == '\0') {
            return;
        }

        debug("[id: %s, count: %u]\r\n", sleep_stats[i].identifier,
                                          sleep_stats[i].count);
    }
}

void sleep_tracker_lock(const char* const filename, int line)
{
    sleep_statistic_t *stat = sleep_tracker_find(filename);

    // Entry for this driver does not exist, create one.
    if (stat == NULL) {
        stat = sleep_tracker_add(filename);
    }

    core_util_atomic_incr_u8(&stat->count, 1);

    debug("LOCK: %s, ln: %i, lock count: %u\r\n", filename, line, deep_sleep_lock);
}

void sleep_tracker_unlock(const char* const filename, int line)
{
    sleep_statistic_t *stat = sleep_tracker_find(filename);<commit_msg>Refactor sleep tracing driver identifier to be pointer to the driver filepath.<commit_after>/* mbed Microcontroller Library
 * Copyright (c) 2017 ARM Limited
 *
 * Licensed 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.
 */

#include "mbed_assert.h"
#include "mbed_power_mgmt.h"
#include "mbed_critical.h"
#include "sleep_api.h"
#include "mbed_error.h"
#include "mbed_debug.h"
#include <limits.h>
#include <stdio.h>

#if DEVICE_SLEEP

// deep sleep locking counter. A target is allowed to deep sleep if counter == 0
static uint16_t deep_sleep_lock = 0U;

#ifdef MBED_SLEEP_TRACING_ENABLED

// Number of drivers that can be stored in the structure
#define STATISTIC_COUNT  10

typedef struct sleep_statistic {
    const char* identifier;
    uint8_t count;
} sleep_statistic_t;

static sleep_statistic_t sleep_stats[STATISTIC_COUNT];

static sleep_statistic_t* sleep_tracker_find(const char *const filename)
{
    for (int i = 0; i < STATISTIC_COUNT; ++i) {
        if (sleep_stats[i].identifier == filename) {
            return &sleep_stats[i];
        }
    }

    return NULL;
}

static sleep_statistic_t* sleep_tracker_add(const char* const filename)
{
    for (int i = 0; i < STATISTIC_COUNT; ++i) {
        if (sleep_stats[i].identifier == NULL) {
            sleep_stats[i].identifier = filename;

            return &sleep_stats[i];
        }
    }

    debug("No free indexes left to use in mbed sleep tracker.\r\n");

    return NULL;
}

static void sleep_tracker_print_stats(void)
{
    debug("Sleep locks held:\r\n");
    for (int i = 0; i < STATISTIC_COUNT; ++i) {
        if (sleep_stats[i].count == 0) {
            continue;
        }

        if (sleep_stats[i].identifier == NULL) {
            return;
        }

        debug("[id: %s, count: %u]\r\n", sleep_stats[i].identifier,
                                         sleep_stats[i].count);
    }
}

void sleep_tracker_lock(const char* const filename, int line)
{
    sleep_statistic_t *stat = sleep_tracker_find(filename);

    // Entry for this driver does not exist, create one.
    if (stat == NULL) {
        stat = sleep_tracker_add(filename);
    }

    core_util_atomic_incr_u8(&stat->count, 1);

    debug("LOCK: %s, ln: %i, lock count: %u\r\n", filename, line, deep_sleep_lock);
}

void sleep_tracker_unlock(const char* const filename, int line)
{
    sleep_statistic_t *stat = sleep_tracker_find(filename);
{'fraction_non_alphanumeric': 0.10473457675753228, 'fraction_numerical': 0.008448908018491949, 'mean_word_length': 3.210738255033557, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4591934', 'n_tokens_mistral': 2042, 'n_tokens_neox': 1974, 'n_words': 608}