automotive-dlt
dlt-kpi-interrupt.c
Go to the documentation of this file.
1 /*
2  * @licence app begin@
3  * SPDX license identifier: MPL-2.0
4  *
5  * Copyright (C) 2011-2015, BMW AG
6  *
7  * This file is part of GENIVI Project DLT - Diagnostic Log and Trace.
8  *
9  * This Source Code Form is subject to the terms of the
10  * Mozilla Public License (MPL), v. 2.0.
11  * If a copy of the MPL was not distributed with this file,
12  * You can obtain one at http://mozilla.org/MPL/2.0/.
13  *
14  * For further information see http://www.genivi.org/.
15  * @licence end@
16  */
17 
27 #include "dlt-kpi-interrupt.h"
28 
30 {
31  if(ctx == NULL)
32  {
33  fprintf(stderr, "%s: Nullpointer parameter (NULL) !\n",__func__);
35  }
36 
37  char buffer[BUFFER_SIZE];
38  *buffer = '\0';
39 
40  char file_buffer[BUFFER_SIZE];
41  char *token, *delim = " \t", *delim2 = " \t\n", *check;
42  int head_line = 1, first_row = 1, cpu_count = 0, column = 0, buffer_offset = 0;
43  DltReturnValue ret;
44 
45  if((ret = dlt_kpi_read_file("/proc/interrupts", file_buffer, BUFFER_SIZE)) < DLT_RETURN_OK) return ret;
46 
47  token = strtok(file_buffer, delim);
48  while(token != NULL)
49  {
50  if(head_line)
51  {
52  if(strlen(token) > 3 && token[0]=='C' && token[1]=='P' && token[2]=='U')
53  cpu_count++;
54  else if(cpu_count <= 0)
55  {
56  fprintf(stderr, "%s: Could not parse CPU count !\n",__func__);
57  return DLT_RETURN_ERROR;
58  }
59  else if(strcmp(token, "\n") == 0)
60  head_line = 0;
61 
62  token = strtok(NULL, delim);
63  }
64  else
65  {
66  int tokenlen = strlen(token);
67  if(token[tokenlen - 1] == ':')
68  {
69  column = 0;
70 
71  if(first_row)
72  first_row = 0;
73  else
74  buffer_offset += snprintf(buffer + buffer_offset, BUFFER_SIZE - buffer_offset, "\n");
75  }
76 
77  if(column == 0) // IRQ number
78  {
79  buffer_offset += snprintf(buffer + buffer_offset, BUFFER_SIZE - buffer_offset, "%.*s;", tokenlen-1, token);
80  }
81  else if(column <= cpu_count)
82  {
83  long int interrupt_count = strtol(token, &check, 10);
84  if(*check != '\0')
85  {
86  fprintf(stderr, "%s: Could not parse interrupt count for CPU !\n",__func__);
87  return DLT_RETURN_ERROR;
88  }
89 
90  buffer_offset += snprintf(buffer + buffer_offset, BUFFER_SIZE - buffer_offset, "cpu%d:%ld;", column - 1, interrupt_count);
91  }
92 
93  column++;
94 
95  token = strtok(NULL, delim2);
96  }
97  }
98 
99  // synchronization message
100  DLT_LOG(*ctx, log_level, DLT_STRING("IRQ"), DLT_STRING("BEG"));
101 
102  DltContextData ctx_data;
103  if((ret = dlt_user_log_write_start(ctx, &ctx_data, log_level)) < DLT_RETURN_OK)
104  {
105  fprintf(stderr, "%s: dlt_user_log_write_start() returned error\n", __func__);
106  return ret;
107  }
108 
109  if((ret = dlt_user_log_write_string(&ctx_data, "IRQ")) < DLT_RETURN_OK)
110  {
111  fprintf(stderr, "%s: dlt_user_log_write_string() returned error\n", __func__);
112  return ret;
113  }
114 
115  token = strtok(buffer, "\n");
116  while(token != NULL)
117  {
118  if(dlt_user_log_write_string(&ctx_data, token) < DLT_RETURN_OK)
119  {
120  /* message buffer full, start new one */
121  if((ret = dlt_user_log_write_finish(&ctx_data)) < DLT_RETURN_OK)
122  {
123  fprintf(stderr, "%s: dlt_user_log_write_finish() returned error\n", __func__);
124  return ret;
125  }
126 
127  if((ret = dlt_user_log_write_start(ctx, &ctx_data, log_level)) < DLT_RETURN_OK)
128  {
129  fprintf(stderr, "%s: dlt_user_log_write_start() returned error\n", __func__);
130  return ret;
131  }
132 
133  if((ret = dlt_user_log_write_string(&ctx_data, "IRQ")) < DLT_RETURN_OK)
134  {
135  fprintf(stderr, "%s: dlt_user_log_write_string() returned error\n", __func__);
136  return ret;
137  }
138  }
139  else
140  token = strtok(NULL, "\n");
141  }
142 
143  if((ret = dlt_user_log_write_finish(&ctx_data)) < DLT_RETURN_OK)
144  {
145  fprintf(stderr, "%s: dlt_user_log_write_finish() returned error\n", __func__);
146  return ret;
147  }
148 
149  // synchronization message
150  DLT_LOG(*ctx, log_level, DLT_STRING("IRQ"), DLT_STRING("END"));
151 
152  return DLT_RETURN_OK;
153 }
DltReturnValue dlt_user_log_write_string(DltContextData *log, const char *text)
Definition: dlt_user.c:2280
DltLogLevelType
Definition: dlt_types.h:102
DltReturnValue
Definition: dlt_types.h:86
#define DLT_STRING(TEXT)
unsigned char buffer[BUFFER_SIZE]
Buffer for dlt file transfer. The size is defined by BUFFER_SIZE.
#define BUFFER_SIZE
#define DLT_LOG(CONTEXT, LOGLEVEL, ARGS...)
DltReturnValue dlt_kpi_log_interrupts(DltContext *ctx, DltLogLevelType log_level)
DltReturnValue dlt_kpi_read_file(char *filename, char *buffer, uint maxLength)
DltReturnValue dlt_user_log_write_finish(DltContextData *log)
Definition: dlt_user.c:1482
#define NULL
Definition: dlt_common.h:232
DltReturnValue dlt_user_log_write_start(DltContext *handle, DltContextData *log, DltLogLevelType loglevel)
Definition: dlt_user.c:1428