Skip to content

Commit d46a55c

Browse files
committed
Add new goto-instrument option print-global-state-size
1 parent d8c5a98 commit d46a55c

File tree

6 files changed

+85
-1
lines changed

6 files changed

+85
-1
lines changed

Diff for: CHANGELOG

+1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
of --undefined-function-is-assume-false
77
* The --fixedbv command-line option has been removed
88
(it was marked deprecated in January 2017)
9+
* GOTO-INSTRUMENT: New option --print-global-state-size
910

1011

1112
5.8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#include <stdint.h>
2+
3+
uint32_t some_global_var;
4+
int8_t other_global_var;
5+
6+
int main(int argc, char *argv[])
7+
{
8+
return 0;
9+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
CORE
2+
main.c
3+
--print-global-state-size
4+
^EXIT=0$
5+
^SIGNAL=0$
6+
^Total size of global objects: 40 bits$
7+
--
8+
^warning: ignoring

Diff for: src/goto-instrument/count_eloc.cpp

+57-1
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,15 @@ Date: December 2012
1616
#include <iostream>
1717
#include <unordered_set>
1818

19-
#include <util/prefix.h>
2019
#include <util/file_util.h>
20+
#include <util/pointer_offset_size.h>
21+
#include <util/prefix.h>
2122

2223
#include <goto-programs/cfg.h>
2324
#include <goto-programs/goto_model.h>
2425

26+
#include <linking/static_lifetime_init.h>
27+
2528
typedef std::unordered_set<irep_idt> linest;
2629
typedef std::unordered_map<irep_idt, linest> filest;
2730
typedef std::unordered_map<irep_idt, filest> working_dirst;
@@ -142,3 +145,56 @@ void print_path_lengths(const goto_modelt &goto_model)
142145
++n_reachable;
143146
std::cout << "Reachable instructions: " << n_reachable << "\n";
144147
}
148+
149+
void print_global_state_size(const goto_modelt &goto_model)
150+
{
151+
const namespacet ns(goto_model.symbol_table);
152+
153+
// if we have a linked object, only account for those that are used
154+
// (slice-global-inits may have been used to avoid unnecessary initialization)
155+
goto_functionst::function_mapt::const_iterator f_it =
156+
goto_model.goto_functions.function_map.find(INITIALIZE_FUNCTION);
157+
const bool has_initialize =
158+
f_it != goto_model.goto_functions.function_map.end();
159+
std::unordered_set<irep_idt> initialized;
160+
161+
if(has_initialize)
162+
{
163+
for(const auto &ins : f_it->second.body.instructions)
164+
{
165+
if(ins.is_assign())
166+
{
167+
const code_assignt &code_assign = to_code_assign(ins.code);
168+
object_descriptor_exprt ode;
169+
ode.build(code_assign.lhs(), ns);
170+
171+
if(ode.root_object().id() == ID_symbol)
172+
{
173+
const symbol_exprt &symbol_expr = to_symbol_expr(ode.root_object());
174+
initialized.insert(symbol_expr.get_identifier());
175+
}
176+
}
177+
}
178+
}
179+
180+
mp_integer total_size = 0;
181+
182+
for(const auto &symbol_entry : goto_model.symbol_table.symbols)
183+
{
184+
const symbolt &symbol = symbol_entry.second;
185+
if(
186+
symbol.is_type || symbol.is_macro || symbol.type.id() == ID_code ||
187+
symbol.type.get_bool(ID_C_constant) ||
188+
has_prefix(id2string(symbol.name), CPROVER_PREFIX) ||
189+
(has_initialize && initialized.find(symbol.name) == initialized.end()))
190+
{
191+
continue;
192+
}
193+
194+
const mp_integer bits = pointer_offset_bits(symbol.type, ns);
195+
if(bits > 0)
196+
total_size += bits;
197+
}
198+
199+
std::cout << "Total size of global objects: " << total_size << " bits\n";
200+
}

Diff for: src/goto-instrument/count_eloc.h

+4
Original file line numberDiff line numberDiff line change
@@ -19,16 +19,20 @@ class goto_modelt;
1919
void count_eloc(const goto_modelt &);
2020
void list_eloc(const goto_modelt &);
2121
void print_path_lengths(const goto_modelt &);
22+
void print_global_state_size(const goto_modelt &);
2223

2324
#define OPT_GOTO_PROGRAM_STATS \
2425
"(count-eloc)" \
2526
"(list-eloc)" \
27+
"(print-global-state-size)" \
2628
"(print-path-lengths)"
2729

2830
#define HELP_GOTO_PROGRAM_STATS \
2931
" --count-eloc count effective lines of code\n" \
3032
" --list-eloc list full path names of lines " \
3133
"containing code\n" \
34+
" --print-global-state-size count the total number of bits of global " \
35+
"objects\n" \
3236
" --print-path-lengths print statistics about control-flow graph " \
3337
"paths\n"
3438

Diff for: src/goto-instrument/goto_instrument_parse_options.cpp

+6
Original file line numberDiff line numberDiff line change
@@ -483,6 +483,12 @@ int goto_instrument_parse_optionst::doit()
483483
return CPROVER_EXIT_SUCCESS;
484484
}
485485

486+
if(cmdline.isset("print-global-state-size"))
487+
{
488+
print_global_state_size(goto_model);
489+
return CPROVER_EXIT_SUCCESS;
490+
}
491+
486492
if(cmdline.isset("list-symbols"))
487493
{
488494
show_symbol_table_brief(goto_model, get_ui());

0 commit comments

Comments
 (0)