summaryrefslogtreecommitdiffstats
path: root/cpukit/sapi/src/profilingreportxml.c
blob: 409fe3d7ebebfd6a5fb12167a03f949f5eea2c14 (plain) (blame)
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
/*
 * Copyright (c) 2014 embedded brains GmbH.  All rights reserved.
 *
 *  embedded brains GmbH
 *  Dornierstr. 4
 *  82178 Puchheim
 *  Germany
 *  <rtems@embedded-brains.de>
 *
 * The license and distribution terms for this file may be
 * found in the file LICENSE in this distribution or at
 * http://www.rtems.com/license/LICENSE.
 */

#ifdef HAVE_CONFIG_H
  #include "config.h"
#endif

#include <rtems/profiling.h>

#ifdef RTEMS_PROFILING

#include <inttypes.h>

typedef struct {
  rtems_profiling_printf printf_func;
  void *printf_arg;
  uint32_t indentation_level;
  const char *indentation;
  int retval;
} context;

static void update_retval(context *ctx, int rv)
{
  if (rv > 0 && ctx->retval >= 0) {
    ctx->retval += rv;
  }
}

static void indent(context *ctx, uint32_t indentation_level)
{
  uint32_t n = ctx->indentation_level + indentation_level;
  uint32_t i;

  for (i = 0; i < n; ++i) {
    int rv = (*ctx->printf_func)(ctx->printf_arg, "%s", ctx->indentation);

    update_retval(ctx, rv);
  }
}

static void report_per_cpu(context *ctx, const rtems_profiling_per_cpu *per_cpu)
{
  rtems_profiling_printf printf_func = ctx->printf_func;
  void *printf_arg = ctx->printf_arg;
  int rv;

  indent(ctx, 1);
  rv = (*printf_func)(
    printf_arg,
    "<PerCPUProfilingReport processorIndex=\"%" PRIu32 "\">\n",
    per_cpu->processor_index
  );
  update_retval(ctx, rv);

  indent(ctx, 2);
  rv = (*printf_func)(
    printf_arg,
    "<MaxThreadDispatchDisabledTime unit=\"ns\">%" PRIu32
      "</MaxThreadDispatchDisabledTime>\n",
    per_cpu->max_thread_dispatch_disabled_time
  );
  update_retval(ctx, rv);

  indent(ctx, 2);
  rv = (*printf_func)(
    printf_arg,
    "<ThreadDispatchDisabledCount>%" PRIu64 "</ThreadDispatchDisabledCount>\n",
    per_cpu->thread_dispatch_disabled_count
  );
  update_retval(ctx, rv);

  indent(ctx, 2);
  rv = (*printf_func)(
    printf_arg,
    "<TotalThreadDispatchDisabledTime unit=\"ns\">%" PRIu64
      "</TotalThreadDispatchDisabledTime>\n",
    per_cpu->total_thread_dispatch_disabled_time
  );
  update_retval(ctx, rv);

  indent(ctx, 2);
  rv = (*printf_func)(
    printf_arg,
    "<MaxInterruptTime unit=\"ns\">%" PRIu32
      "</MaxInterruptTime>\n",
    per_cpu->max_interrupt_time
  );
  update_retval(ctx, rv);

  indent(ctx, 2);
  rv = (*printf_func)(
    printf_arg,
    "<MaxInterruptDelay unit=\"ns\">%" PRIu32 "</MaxInterruptDelay>\n",
    per_cpu->max_interrupt_delay
  );
  update_retval(ctx, rv);

  indent(ctx, 2);
  rv = (*printf_func)(
    printf_arg,
    "<InterruptCount>%" PRIu64 "</InterruptCount>\n",
    per_cpu->interrupt_count
  );
  update_retval(ctx, rv);

  indent(ctx, 2);
  rv = (*printf_func)(
    printf_arg,
    "<TotalInterruptTime unit=\"ns\">%" PRIu64 "</TotalInterruptTime>\n",
    per_cpu->total_interrupt_time
  );
  update_retval(ctx, rv);

  indent(ctx, 1);
  rv = (*printf_func)(
    printf_arg,
    "</PerCPUProfilingReport>\n"
  );
  update_retval(ctx, rv);
}

static void report(void *arg, const rtems_profiling_data *data)
{
  context *ctx = arg;

  switch (data->header.type) {
    case RTEMS_PROFILING_PER_CPU:
      report_per_cpu(ctx, &data->per_cpu);
      break;
  }
}

#endif /* RTEMS_PROFILING */

int rtems_profiling_report_xml(
  const char *name,
  rtems_profiling_printf printf_func,
  void *printf_arg,
  uint32_t indentation_level,
  const char *indentation
)
{
#ifdef RTEMS_PROFILING
  context ctx_instance = {
    .printf_func = printf_func,
    .printf_arg = printf_arg,
    .indentation_level = indentation_level,
    .indentation = indentation,
    .retval = 0
  };
  context *ctx = &ctx_instance;
  int rv;

  indent(ctx, 0);
  rv = (*printf_func)(printf_arg, "<ProfilingReport name=\"%s\">\n", name);
  update_retval(ctx, rv);

  rtems_profiling_iterate(report, ctx);

  indent(ctx, 0);
  rv = (*printf_func)(printf_arg, "</ProfilingReport>\n");
  update_retval(ctx, rv);

  return ctx->retval;
#else /* RTEMS_PROFILING */
  (void) name;
  (void) printf_func;
  (void) printf_arg;
  (void) indentation_level;
  (void) indentation;

  return 0;
#endif /* RTEMS_PROFILING */
}