In a pure object-oriented system, all computations start by sending messages to objects. We can thus define an <em>event</em> as the sending of a message to an object. An event can then be specified by the tuple <code>(Object, Message, Sender)</code>. This definition can be refined by interpreting the sending of a message and the return of the control to the object that has sent the message as two distinct events. We call these events respectively <code>before</code> and <code>after</code>. Therefore, we end up by representing an event by the tuple <code>(Event, Object, Message, Sender)</code>. For instance, if we send the message:
Note that the second event is only generated if the message succeeds. If the message as a goal have multiple solutions, then one <code>after</code> event will be generated for each solution.
</p>
<p>
Events are automatically generated by the message sending mechanisms for each public message sent using the <atitle="Consult reference manual"href="../refman/control/to_object2.html"><code>::/2</code></a> operator.
A monitor is an object that reacts whenever a spied event occurs. The monitor actions are defined by two event handlers: <atitle="Consult reference manual"href="../refman/methods/before3.html"><code>before/3</code></a> for <code>before</code> events and <atitle="Consult reference manual"href="../refman/methods/after3.html"><code>after/3</code></a> for <code>after</code> events. These predicates are automatically called by the message sending mechanisms when an event registered for the monitor occurs.
</p>
<p>
In our example, we need a way to get the current time before and after we process a message. We will assume that we have a <code>time</code> object implementing a <code>cpu_time/1</code> predicate that returns the current CPU time for the Prolog session:
Our profiler will be named <code>stop_watch</code>. It must define event handlers for the <code>before</code> and <code>after</code> events that will print the event description (object, message, and sender) and the current time:
After compiling and loading the <code>stop_watch</code> object (and the objects that we want to profile), we can use the <atitle="Consult reference manual"href="../refman/builtins/define_events5.html"><code>define_events/5</code></a> built-in predicate to set up our profiler. For example, to profile all messages that are sent to the object <code>foo</code>, we need to call the goal:
This call will register <code>stop_watch</code> as a monitor to all messages sent to object <code>foo</code>, for both <code>before</code> and <code>after</code> events. Note that we say "as a monitor", not "the monitor": we can have any number of monitors over the same events.
</p>
<p>
From now on, every time we sent a message to <code>foo</code>, the <code>stop_watch</code> monitor will print the starting and ending times for the message execution. For instance:
To stop profiling the messages sent to <code>foo</code> we use the <atitle="Consult reference manual"href="../refman/builtins/abolish_events5.html"><code>abolish_events/5</code></a>
<li>An event is defined as the sending of a (public) message to an object.</li>
</ul>
<ul>
<li>There are two kinds of events: <code>before</code> events, generated before a message is processed, and <code>after</code> events, generated after the message processing completed successfully.</li>
</ul>
<ul>
<li>Any object can be declared as a monitor to any event.</li>
</ul>
<ul>
<li>A monitor defines event handlers, the predicates <atitle="Consult reference manual"href="../refman/methods/before3.html"><code>before/3</code></a> and <atitle="Consult reference manual"href="../refman/methods/after3.html"><code>after/3</code></a>, that are automatically called by the runtime engine when a spied event occurs.</li>
</ul>
<ul>
<li>Three built-in predicates, <atitle="Consult reference manual"href="../refman/builtins/define_events5.html"><code>define_events/5</code></a>, <atitle="Consult reference manual"href="../refman/builtins/current_event5.html"><code>current_event/5</code></a>, and <atitle="Consult reference manual"href="../refman/builtins/abolish_events5.html"><code>abolish_events/5</code></a>, enables us define, query, and abolish both events and monitors.</li>