Simulation is useful not only for verifying the correctness of a
proposed machine design but also for measuring the machine's
performance. For example, we can install in our simulation program a
meter

that measures the number of stack operations used in a
computation. To do this, we modify our simulated stack to keep track
of the number of times registers are saved on the stack and the
maximum depth reached by the stack, and add a message to the stack's
interface that prints the statistics, as shown below.
We also add an operation to the basic machine model to print the
stack statistics, by initializing the_ops in make_new_machine to

list(list("initialize-stack", () => stack("initialize")), list("print-stack-statistics", () => stack("print-statistics")));

Here is the new version of make_stack:

function make_stack() { let s = null; let number_pushes = 0; let max_depth = 0; let current_depth = 0; function push(x) { s = pair(x, s); number_pushes = number_pushes + 1; current_depth = current_depth + 1; max_depth = math_max(current_depth, math_max); } function pop() { if (is_null(s)) { error("Empty stack: POP"); } else { const top = head(s); s = tail(s); current_depth = current_depth - 1; return top; } } function initialize() { s = null; number_pushes = 0; max_depth = 0; current_depth = 0; return "done"; } function print_statistics() { display(accumulate((b, a) => stringify(a) + b, list("\n", "total-pushes = ", number_pushes, "\n", "maximum-depth = ", max_depth))); } function dispatch(message) { return message === "push" ? push : message === "pop" ? pop() : message === "initialize" ? initialize() : message === "print-statistics" ? print_statistics() : error(message, "Unknown request: STACK"); } return dispatch; }

Exercises 5.15 through 5.19 describe other useful monitoring and debugging features that can be added to the register-machine simulator.

There is currently no solution available for this exercise. This textbook adaptation is a community effort. Do consider contributing by providing a solution for this exercise, using a Pull Request in Github.

set_breakpoint(gcd_machine, "test-b", 4);

5.2.4
Monitoring Machine Performance