summaryrefslogtreecommitdiff
path: root/src/kpi/dlt-kpi-interrupt.c
blob: 7274cc072f99d53223e922a974fb422d7562e831 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
/*
 * @licence app begin@
 * SPDX license identifier: MPL-2.0
 *
 * Copyright (C) 2011-2015, BMW AG
 *
 * This file is part of GENIVI Project DLT - Diagnostic Log and Trace.
 *
 * This Source Code Form is subject to the terms of the
 * Mozilla Public License (MPL), v. 2.0.
 * If a copy of the MPL was not distributed with this file,
 * You can obtain one at http://mozilla.org/MPL/2.0/.
 *
 * For further information see http://www.genivi.org/.
 * @licence end@
 */

/*!
 * \author Sven Hassler <sven_hassler@mentor.com>
 *
 * \copyright Copyright © 2011-2015 BMW AG. \n
 * License MPL-2.0: Mozilla Public License version 2.0 http://mozilla.org/MPL/2.0/.
 *
 * \file dlt-kpi-interrupt.c
 */

#include "dlt-kpi-interrupt.h"

DltReturnValue dlt_kpi_log_interrupts(DltContext *ctx, DltLogLevelType log_level)
{
    if (ctx == NULL) {
        fprintf(stderr, "%s: Nullpointer parameter (NULL) !\n", __func__);
        return DLT_RETURN_WRONG_PARAMETER;
    }

    char buffer[BUFFER_SIZE];
    *buffer = '\0';

    char file_buffer[BUFFER_SIZE];
    char *token, *delim = " \t", *delim2 = " \t\n", *check;
    int head_line = 1, first_row = 1, cpu_count = 0, column = 0, buffer_offset = 0;
    DltReturnValue ret;

    if ((ret = dlt_kpi_read_file("/proc/interrupts", file_buffer, BUFFER_SIZE)) < DLT_RETURN_OK) return ret;

    token = strtok(file_buffer, delim);

    while (token != NULL) {
        if (head_line) {
            if ((strlen(token) > 3) && (token[0] == 'C') && (token[1] == 'P') && (token[2] == 'U')) {
                cpu_count++;
            }
            else if (cpu_count <= 0)
            {
                fprintf(stderr, "%s: Could not parse CPU count !\n", __func__);
                return DLT_RETURN_ERROR;
            }
            else if (strcmp(token, "\n") == 0)
            {
                head_line = 0;
            }

            token = strtok(NULL, delim);
        }
        else {
            int tokenlen = strlen(token);

            if (token[tokenlen - 1] == ':') {
                column = 0;

                if (first_row)
                    first_row = 0;
                else
                    buffer_offset += snprintf(buffer + buffer_offset, BUFFER_SIZE - buffer_offset, "\n");
            }

            if (column == 0) { /* IRQ number */
                buffer_offset += snprintf(buffer + buffer_offset,
                                          BUFFER_SIZE - buffer_offset,
                                          "%.*s;",
                                          tokenlen - 1,
                                          token);
            }
            else if (column <= cpu_count)
            {
                long int interrupt_count = strtol(token, &check, 10);

                if (*check != '\0') {
                    fprintf(stderr, "%s: Could not parse interrupt count for CPU !\n", __func__);
                    return DLT_RETURN_ERROR;
                }

                buffer_offset += snprintf(buffer + buffer_offset,
                                          BUFFER_SIZE - buffer_offset,
                                          "cpu%d:%ld;",
                                          column - 1,
                                          interrupt_count);
            }

            column++;

            token = strtok(NULL, delim2);
        }
    }

    /* synchronization message */
    DLT_LOG(*ctx, log_level, DLT_STRING("IRQ"), DLT_STRING("BEG"));

    DltContextData ctx_data;

    if ((ret = dlt_user_log_write_start(ctx, &ctx_data, log_level)) < DLT_RETURN_OK) {
        fprintf(stderr, "%s: dlt_user_log_write_start() returned error\n", __func__);
        return ret;
    }

    if ((ret = dlt_user_log_write_string(&ctx_data, "IRQ")) < DLT_RETURN_OK) {
        fprintf(stderr, "%s: dlt_user_log_write_string() returned error\n", __func__);
        return ret;
    }

    token = strtok(buffer, "\n");

    while (token != NULL) {
        if (dlt_user_log_write_string(&ctx_data, token) < DLT_RETURN_OK) {
            /* message buffer full, start new one */
            if ((ret = dlt_user_log_write_finish(&ctx_data)) < DLT_RETURN_OK) {
                fprintf(stderr, "%s: dlt_user_log_write_finish() returned error\n", __func__);
                return ret;
            }

            if ((ret = dlt_user_log_write_start(ctx, &ctx_data, log_level)) < DLT_RETURN_OK) {
                fprintf(stderr, "%s: dlt_user_log_write_start() returned error\n", __func__);
                return ret;
            }

            if ((ret = dlt_user_log_write_string(&ctx_data, "IRQ")) < DLT_RETURN_OK) {
                fprintf(stderr, "%s: dlt_user_log_write_string() returned error\n", __func__);
                return ret;
            }
        }
        else {
            token = strtok(NULL, "\n");
        }
    }

    if ((ret = dlt_user_log_write_finish(&ctx_data)) < DLT_RETURN_OK) {
        fprintf(stderr, "%s: dlt_user_log_write_finish() returned error\n", __func__);
        return ret;
    }

    /* synchronization message */
    DLT_LOG(*ctx, log_level, DLT_STRING("IRQ"), DLT_STRING("END"));

    return DLT_RETURN_OK;
}