Contiki 2.5
sprofiling.c
1 /*
2  * Copyright (c) 2011, Daniel Willmann <daniel@totalueberwachung.de>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  * notice, this list of conditions and the following disclaimer in the
12  * documentation and/or other materials provided with the distribution.
13  * 3. Neither the name of the Institute nor the names of its contributors
14  * may be used to endorse or promote products derived from this software
15  * without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  *
29  * This file is part of the Contiki operating system.
30  *
31  * Statistical profiler for Contiki - it will not be able to profile interrupts
32  * or parts where interrupts are disabled.
33  *
34  * @(#)$$
35  */
36 
37 #include <stdio.h>
38 #include <string.h>
39 
40 #include "contiki.h"
41 #include "sys/sprofiling.h"
42 
43 #ifdef SPROFILES_CONF_MAX
44 #define MAX_PROFILES SPROFILES_CONF_MAX
45 #else
46 #define MAX_PROFILES 1
47 #endif /* MAX_CONF_PROFILES */
48 
49 static struct sprofile_t stat_profile;
50 static struct sprofile_site_t stat_site[MAX_PROFILES];
51 
52 void sprofiling_report(const char* name, uint8_t pretty)
53 {
54  int i;
55 
56  /* The parser would be confused if the name contains colons or newlines, so disallow */
57  if (!name || strchr(name, ':') || strchr(name, '\r') || strchr(name, '\n')) {
58  printf("The profile report name is invalid\n");
59  name = "invalid";
60  }
61 
62  if (pretty)
63  printf("\nSPROF: \"%s\" %u sites %u max_sites %lu samples\npc:calls\n", name, stat_profile.num_sites, stat_profile.max_sites, stat_profile.num_samples);
64  else
65  printf("\nSPROF:%s:%u:%u:%lu\n", name, stat_profile.num_sites, stat_profile.max_sites, stat_profile.num_samples);
66 
67  for(i=0; i<stat_profile.num_sites;i++) {
68  printf("%p:%u\n", stat_profile.sites[i].addr, stat_profile.sites[i].calls);
69  }
70 }
71 
72 struct sprofile_t *sprofiling_get()
73 {
74  return &stat_profile;
75 }
76 
77 inline void sprofiling_add_sample(void *pc)
78 {
79  uint8_t i;
80  for (i=0;i<stat_profile.num_sites;i++) {
81  if (stat_profile.sites[i].addr == pc)
82  break;
83  }
84  if (i<stat_profile.max_sites) {
85  if (i==stat_profile.num_sites) {
86  stat_profile.sites[i].addr = pc;
87  stat_profile.sites[i].calls = 1;
88  stat_profile.num_sites++;
89  } else {
90  stat_profile.sites[i].calls++;
91  }
92  stat_profile.num_samples++;
93  }
94 }
95 
96 void sprofiling_init(void)
97 {
98  stat_profile.sites = stat_site;
99  stat_profile.max_sites = MAX_PROFILES;
100 
101  sprofiling_arch_init();
102 }
103 
104 inline void sprofiling_start(void)
105 {
106  sprofiling_arch_start();
107 }
108 
109 inline void sprofiling_stop(void)
110 {
111  sprofiling_arch_stop();
112 }