ssm  0.0.2
Runtime Library for the Sparse Synchronous Model
ssm-internal.h
Go to the documentation of this file.
1 /** @file ssm-internal.h
2  * @brief The internal interface of the SSM runtime.
3  *
4  * This header file contains definitions and declarations that should not be
5  * exposed to user code.
6  *
7  * @author Stephen Edwards (sedwards-lab)
8  * @author John Hui (j-hui)
9  */
10 #ifndef _SSM_SCHED_H
11 #define _SSM_SCHED_H
12 
13 #include <ssm-platform.h>
14 #include <ssm.h>
15 
16 /** @ingroup error
17  * @brief Throw an internal error.
18  *
19  * @platformonly
20  *
21  * @param cond the condition to assert.
22  */
23 #define SSM_ASSERT(cond) \
24  do \
25  if (!(cond)) \
26  SSM_THROW(SSM_INTERNAL_ERROR); \
27  while (0)
28 
29 /** @ingroup time
30  * @brief The time of the next event in the event queue.
31  *
32  * Used to determine whether and when to invoke ssm_tick().
33  *
34  * @platformonly
35  *
36  * @returns the next event time, or #SSM_NEVER if the event queue is empty.
37  */
39 
40 /** @ingroup act
41  * @brief Whether there are still active processes in the activation queue.
42  *
43  * @platformonly
44  *
45  * @returns true if there is at least one active process, false otherwise.
46  */
47 bool ssm_active(void);
48 
49 /** @ingroup time
50  * @brief Reset the scheduler.
51  *
52  * Set #now to 0; clear the event and activation record queues.
53  *
54  * This does not need to be called before calling ssm_tick() for the first
55  * time; the global state automatically starts initialized.
56  *
57  * @platformonly
58  */
59 void ssm_reset(void);
60 
61 /** @ingroup time
62  * @brief Advance the current model time.
63  *
64  * @a next must be later than ssm_now(), and earlier than or equal to
65  * ssm_next_event_time().
66  *
67  * Exposed so that platform code can perform external variable updates, to
68  * implement external inputs.
69  *
70  * @platformonly
71  *
72  * @param next the time to advance to.
73  *
74  * @throws SSM_INVALID_TIME @a next is earlier than or equal to #now.
75  * @throws SSM_NOT_READY @a next is later than the earliest queued event.
76  */
77 void ssm_set_now(ssm_time_t next);
78 
79 /** @ingroup sv
80  * @brief Perform a (delayed) update on a variable.
81  *
82  * Schedules all routine activation records sensitive to @a sv in the
83  * activation queue.
84  *
85  * Should only be called if the variable is scheduled to be updated #now.
86  *
87  * Exposed so that platform code can perform external variable updates.
88  *
89  * @platformonly
90  *
91  * @param sv the variable.
92  *
93  * @throws SSM_NOT_READY @a sv was not scheduled to be updated #now.
94  */
95 void ssm_update(ssm_sv_t *sv);
96 
97 /** @ingroup sv
98  * @brief Unschedule any pending events on a variable.
99  *
100  * Should be called before the variable is dropped.
101  *
102  * Nothing happens if the variable does not have a pending event.
103  *
104  * @platformonly
105  *
106  * @param var the variable.
107  */
108 void ssm_unschedule(ssm_sv_t *var);
109 
110 /** @ingroup act
111  * @brief Run the system for the next scheduled instant.
112  *
113  * If there is nothing left to run in the current instant, advance #now to the
114  * time of the earliest event in the queue, if any.
115  *
116  * Removes every event at the head of the event queue scheduled for #now,
117  * updates each variable's current value from its later value, and schedules
118  * all sensitive triggers in the activation queue. Finally, execute the step
119  * functions of all scheduled routines, in order of priority.
120  *
121  * Should only be called if there are activation records to execute #now,
122  * or if #now is earlier than ssm_next_event_time().
123  *
124  * @platformonly
125  *
126  * @throws SSM_INTERNAL_ERROR there are stale events in the queue before #now.
127  */
128 void ssm_tick(void);
129 
130 /**
131  * @addtogroup mem
132  * @{
133  */
134 
135 /** @brief Initializes the underlying allocator system.
136  *
137  * Memory pages are requested from the platform/OS on-demand via the provided
138  * @a alloc_page_handler. This handler must return a pointer to the beginning
139  * of a range of memory of size #SSM_MEM_PAGE_SIZE, and the page @em must be
140  * zeroed out. These pages are assumed to never be freed.
141  *
142  * To support arbitrarily large allocations, SSM's allocator uses @a
143  * alloc_mem_handler to allocate memory, and @a free_mem_handler to release it.
144  * There are no requirements on the contents of memory allocated with @a
145  * alloc_mem_handler. These handlers may also assume they will not be invoked
146  * to request memory ranges of less than #SSM_MEM_POOL_MAX bytes.
147  *
148  * If the allocator is compiled with valgrind support (i.e., @a USE_VALGRIND is
149  * defined), it will perform a leak-check summary, to checkpoint how much
150  * memory has already been allocated.
151  *
152  * @platformonly
153  *
154  * @param alloc_page_handler allocates pages.
155  * @param alloc_mem_handler allocates arbitrarily large.
156  * @param free_mem_handler frees memory allocated with @a alloc_mem_handler.
157  */
158 void ssm_mem_init(void *(*alloc_page_handler)(void),
159  void *(*alloc_mem_handler)(size_t),
160  void (*free_mem_handler)(void *, size_t));
161 
162 /** @brief Tears down the underlying allocator system.
163  *
164  * If the allocator is compiled with valgrind support (i.e., @a USE_VALGRIND is
165  * defined), it will perform a full leak-check summary, to report how much
166  * memory has been leaked since ssm_mem_init().
167  *
168  * @TODO this doesn't actually call @a free_page_handler yet. It still needs to
169  * be implemented, perhaps with the help of a superblock header to keep
170  * track of all pages allocated for each mempool
171  *
172  * @platformonly
173  *
174  * @param free_page_handler frees pages allocated with @a alloc_page_handler.
175  */
176 void ssm_mem_destroy(void (*free_page_handler)(void *));
177 
178 #ifndef SSM_MEM_POOL_MIN
179 /** @brief Block size of the smallest memory pool.
180  *
181  * Must be strictly greater than the word size, i.e., the size of #ssm_word_t.
182  *
183  * @platformonly
184  */
185 #define SSM_MEM_POOL_MIN 16
186 #endif
187 
188 #if SSM_MEM_POOL_MIN < SSM_POINTER_SIZE
189 #error SSM_MEM_POOL_MIN must be larger than word size.
190 #endif
191 
192 #ifndef SSM_MEM_POOL_FACTOR_BASE2
193 /** @brief Factor between each successive memory pool size, in base 2.
194  *
195  * Must be strictly greater than 0.
196  *
197  * @platformonly
198  */
199 #define SSM_MEM_POOL_FACTOR_BASE2 2
200 #endif
201 
202 #if SSM_MEM_POOL_FACTOR_BASE2 < 1
203 #error SSM_MEM_POOL_FACTOR_BASE2 must be strictly greater than 0.
204 #endif
205 
206 #ifndef SSM_MEM_POOL_COUNT
207 /** @brief Number of memory pools.
208  *
209  * Must be strictly greater than 0.
210  *
211  * @platformonly
212  */
213 #define SSM_MEM_POOL_COUNT 4
214 #endif
215 
216 #if SSM_MEM_POOL_COUNT < 1
217 #error SSM_MEM_POOL_COUNT must be strictly greater than 0.
218 #endif
219 
220 /** @brief Compute the size of a memory pool.
221  *
222  * @platformonly
223  *
224  * @param pool the 0-index of the memory pool.
225  * @returns the size of the memory pool at position @a pool.
226  */
227 #define SSM_MEM_POOL_SIZE(pool) \
228  (SSM_MEM_POOL_MIN << (SSM_MEM_POOL_FACTOR_BASE2 * pool))
229 
230 /** @brief The size of the largest memory pool.
231  *
232  * @platformonly
233  */
234 #define SSM_MEM_POOL_MAX SSM_MEM_POOL_SIZE(SSM_MEM_POOL_COUNT - 1)
235 
236 #ifndef SSM_MEM_PAGE_SIZE
237 /** @brief The size of a memory page; must be greater than #SSM_MEM_POOL_MAX.
238  *
239  * @platformonly
240  */
241 #define SSM_MEM_PAGE_SIZE SSM_MEM_POOL_SIZE(SSM_MEM_POOL_COUNT)
242 #endif
243 
244 #ifdef CONFIG_MEM_STATS
245 
246 /** @brief Statistics for a heap page pool; used in #ssm_mem_statistics_t.
247  *
248  * @note Define CONFIG_MEM_STATS to enable this.
249  */
250 typedef struct ssm_mem_statistics_pool {
251  size_t block_size; /**< Size of this pool's blocks */
252  size_t pages_allocated; /**< Number of pages allocated to this pool */
253  size_t free_list_length; /**< Length of the free list */
255 
256 /** @brief Statistics for the heap; filled with #ssm_mem_statistics_collect().
257  *
258  * A collection of satistics collected by #ssm_mem_statistics_collect()
259  * and designed to be printed by a function you supply.
260  *
261  * @note Define CONFIG_MEM_STATS to enable this.
262  */
263 typedef struct ssm_mem_statistics {
264  size_t sizeof_ssm_mm; /**< Size of per-object memory management header */
265  size_t page_size; /**< Bytes in a memory page */
266  size_t pages_allocated; /**< Number of pages that have been allocated */
267  size_t objects_allocated; /**< Total number of allocated objects */
268  size_t objects_freed; /**< Total number of object free events */
269  size_t live_objects; /**< Number of live objects **/
270  size_t pool_count; /**< Number of memory pools */
271  ssm_mem_statistics_pool_t pool[32]; /**< Size of the blocks in each pool */
273 
274 /** @brief Collect and return statistics about the heap.
275  *
276  * @note Define CONFIG_MEM_STATS to enable this.
277  *
278  * @param stats non-NULL pointer to a #ssm_mem_statistics_t to be filled in
279  */
281 
282 #endif /* CONFIG_MEM_STATS */
283 
284 /** @} */
285 
286 #ifdef USE_VALGRIND
287 #include <valgrind/memcheck.h>
288 #endif
289 
290 #endif /* _SSM_SCHED_H */
void ssm_tick(void)
Run the system for the next scheduled instant.
bool ssm_active(void)
Whether there are still active processes in the activation queue.
struct ssm_mem_statistics ssm_mem_statistics_t
Statistics for the heap; filled with ssm_mem_statistics_collect().
void ssm_mem_destroy(void(*free_page_handler)(void *))
Tears down the underlying allocator system.
Definition: ssm-mem.c:156
struct ssm_mem_statistics_pool ssm_mem_statistics_pool_t
Statistics for a heap page pool; used in ssm_mem_statistics_t.
void ssm_mem_statistics_collect(ssm_mem_statistics_t *stats)
Collect and return statistics about the heap.
Definition: ssm-mem.c:378
void ssm_mem_init(void *(*alloc_page_handler)(void), void *(*alloc_mem_handler)(size_t), void(*free_mem_handler)(void *, size_t))
Initializes the underlying allocator system.
Definition: ssm-mem.c:136
void ssm_unschedule(ssm_sv_t *var)
Unschedule any pending events on a variable.
void ssm_update(ssm_sv_t *sv)
Perform a (delayed) update on a variable.
void ssm_reset(void)
Reset the scheduler.
void ssm_set_now(ssm_time_t next)
Advance the current model time.
ssm_time_t ssm_next_event_time(void)
The time of the next event in the event queue.
uint64_t ssm_time_t
Absolute time; never to overflow.
Definition: ssm.h:399
Interface to the SSM runtime.
Statistics for a heap page pool; used in ssm_mem_statistics_t.
Definition: ssm-internal.h:250
size_t pages_allocated
Number of pages allocated to this pool.
Definition: ssm-internal.h:252
size_t free_list_length
Length of the free list.
Definition: ssm-internal.h:253
size_t block_size
Size of this pool's blocks.
Definition: ssm-internal.h:251
Statistics for the heap; filled with ssm_mem_statistics_collect().
Definition: ssm-internal.h:263
size_t pages_allocated
Number of pages that have been allocated.
Definition: ssm-internal.h:266
size_t sizeof_ssm_mm
Size of per-object memory management header.
Definition: ssm-internal.h:264
ssm_mem_statistics_pool_t pool[32]
Size of the blocks in each pool.
Definition: ssm-internal.h:271
size_t live_objects
Number of live objects.
Definition: ssm-internal.h:269
size_t objects_allocated
Total number of allocated objects.
Definition: ssm-internal.h:267
size_t pool_count
Number of memory pools.
Definition: ssm-internal.h:270
size_t objects_freed
Total number of object free events.
Definition: ssm-internal.h:268
size_t page_size
Bytes in a memory page.
Definition: ssm-internal.h:265
A scheduled variable that supports scheduled updates with triggers.
Definition: ssm.h:588