Arduino Libraries
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
ram_report.hpp
Go to the documentation of this file.
1 // Author: Mario S. Könz <mskoenz@gmx.net>
2 // Date: 25.05.2013 19:22:02 EDT
3 // File: ram_report.hpp
4 
5 /* This program is free software. It comes without any warranty, to
6  * the extent permitted by applicable law. You can redistribute it
7  * and/or modify it under the terms of the Do What The Fuck You Want
8  * To Public License, Version 2, as published by Sam Hocevar. See
9  * http://www.wtfpl.net/ or COPYING for more details. */
10 
11 #ifndef __RAM_REPORT_HEADER
12 #define __RAM_REPORT_HEADER
13 
14 #include <ustd/iostream.hpp>
15 
16 extern int __data_start
17  , __data_end
18  , __bss_start
19  , __bss_end
20  , __heap_start
21  , __brkval
22  , __flp
23  ;
24 
25 namespace diag {
26  int ram_capacity() {
27  return (RAMEND - int(&__data_start));
28  }
29  int free_ram() {
30  int v;
31  return (int) &v - (int(__brkval) == 0 ? int(&__heap_start) : int(__brkval));
32  }
33  void print_stack() {
34 
35  int16_t freeptr = __flp;
36  int16_t pos = int16_t(&__heap_start);
37  int16_t const end = int16_t(__brkval);
38 
39  ustd::cout << GREEN << "used heap: " << GREENB << end - pos << NONE << ustd::endl;
40  ustd::cout << YELLOW << "free list " << RED << "size bytes " << MAGENTA << "pointer " << NONE << "used space" << ustd::endl;
41  uint16_t size = 0;
42  uint8_t pointer_print = 0;
43  auto color = NONE;
44  ustd::cout << ustd::setfill('0');
45  for(; pos < end; ++pos) {
46  if(size == 0) {
47  if(pos == freeptr) {
48  color = YELLOW;
49  pointer_print = 2;
50  ustd::cout << color;
51  freeptr = *(uint8_t*)(pos + 2) + (*(uint8_t*)(pos + 3) << 8);
52  } else {
53  color = NONE;
54  ustd::cout << color;
55  }
56 
57 
58  size = *(uint8_t*)pos + (*(uint8_t*)(pos + 1) << 8);
59 
60  ustd::cout << MAGENTAB << ustd::setw(3) << pos << color << "/";
61  ustd::cout << REDB << ustd::setw(3) << size << color << ": ";
62  ustd::cout.flush();
63  ustd::cout << RED <<ustd::setw(3) << *(uint8_t*)pos << " ";
64  ustd::cout << ustd::setw(3) << *(uint8_t*)(pos + 1) << color << " ";
65 
66  pos += 2;
67  }
68  if(pointer_print != 0){
70  --pointer_print;
71  }
72  else
73  ustd::cout << color;
74 
75  ustd::cout << ustd::setw(3) << *(uint8_t*)pos << " ";
76 
77  size--;
78  if(size == 0)
80  }
81  ustd::cout(1) << ustd::endl;
82  }
83  int used_ram() {
84  return ram_capacity() - free_ram();
85  }
86  void ram_report() {
87  uint8_t split = 32;
88  uint8_t incr = 8;
89  uint8_t count = 0;
90 
91  ustd::cout << F("======= RAM Report =======================================") << ustd::endl;
92 
93  for(int i = int(&__data_start); i < RAMEND; i += incr) {
94  if(i < int(&__data_end))
95  ustd::cout << YELLOWB << F("|");
96  else if(i < int(&__bss_end))
97  ustd::cout << REDB << F("|");
98  else if(i < int(__brkval))
99  ustd::cout << WHITEB << F("|");
100  else if(i < int(SP))
101  ustd::cout << GREEN << F(".");
102  else if(i < RAMEND)
103  ustd::cout << BLUEB << F("|");
104 
105  if((i - int(&__data_start) + incr) % (split * incr) == 0) {
106  ustd::cout << NONE << " ";
107  #define PRINT_RAM(var, color) \
108  ustd::cout << color << ustd::setw(6) << F(#var) << F(": ") << ustd::setw(4) << var \
109  << F(" bytes (") << ustd::setw(2) << int(var * 100.0 / 2047 + 0.5) << F("%)");
110  //ram_capacity()
111  if(count == 1) {
112  uint16_t const data = int(&__data_end) - int(&__data_start);
113  PRINT_RAM(data, YELLOWB)
114  } else if(count == 2) {
115  uint16_t const bss = int(&__bss_end) - int(&__bss_start);
116  PRINT_RAM(bss, REDB)
117  } else if(count == 3) {
118  uint16_t const heap = int(__brkval) - int(&__heap_start);
119  PRINT_RAM(heap, WHITEB)
120  } else if(count == 4) {
121  uint16_t const free = SP - int(__brkval);
122  PRINT_RAM(free, GREENB)
123  } else if(count == 5) {
124  uint16_t stack = RAMEND - SP;
125  PRINT_RAM(stack, BLUEB)
126  }
127 
128  ustd::cout << NONE << ustd::endl;
129  ++count;
130  }
131  }
132  ustd::cout << F("==========================================================") << ustd::endl;
133  }
134 }//end namespace diag
135 #endif //__ram_REPORT_HEADER
void ram_report()
Definition: ram_report.hpp:86
int used_ram()
Definition: ram_report.hpp:83
#define GREEN
Definition: color.hpp:15
int __data_start
int __flp
int ram_capacity()
Definition: ram_report.hpp:26
#define YELLOWB
Definition: color.hpp:18
#define WHITEB
Definition: color.hpp:26
#define BLUEB
Definition: color.hpp:20
#define RED
Definition: color.hpp:13
int free_ram()
Definition: ram_report.hpp:29
#define GREENB
Definition: color.hpp:16
#define PRINT_RAM(var, color)
#define MAGENTA
Definition: color.hpp:21
int __bss_end
#define NONE
Definition: color.hpp:37
struct ustd::endl_class endl
int __bss_start
#define MAGENTAB
Definition: color.hpp:22
int __data_end
int __heap_start
int __brkval
#define REDB
Definition: color.hpp:14
#define F(x)
Definition: ustd_generic.hpp:15
void print_stack()
Definition: ram_report.hpp:33
ustd::cout_class cout
cout_class & flush()
Definition: iostream.hpp:91
#define YELLOW
Definition: color.hpp:17