add Outputable instance for OccIfaceEq
[ghc-hetmet.git] / rts / parallel / GranSimRts.h
1 /* --------------------------------------------------------------------------
2    Time-stamp: <Tue Mar 06 2001 00:18:30 Stardate: [-30]6285.06 hwloidl>
3
4    Variables and functions specific to GranSim.
5    ----------------------------------------------------------------------- */
6
7 #ifndef GRANSIM_RTS_H
8 #define GRANSIM_RTS_H
9
10 //@node Headers for GranSim objs used only in the RTS internally, , ,
11 //@section Headers for GranSim objs used only in the RTS internally
12
13 //@menu
14 //* Event queue::               
15 //* Spark handling routines::   
16 //* Processor related stuff::   
17 //* Local types::               
18 //* Statistics gathering::      
19 //* Prototypes::                
20 //@end menu
21 //*/ fool highlight
22
23 //@node Event queue, Spark handling routines, Headers for GranSim objs used only in the RTS internally, Headers for GranSim objs used only in the RTS internally
24 //@subsection Event queue
25
26 #if defined(GRAN) || defined(PAR)
27 /* Granularity event types for output (see DumpGranEvent) */
28 typedef enum GranEventType_ {
29     GR_START = 0, GR_STARTQ, 
30     GR_STEALING, GR_STOLEN, GR_STOLENQ, 
31     GR_FETCH, GR_REPLY, GR_BLOCK, GR_RESUME, GR_RESUMEQ,
32     GR_SCHEDULE, GR_DESCHEDULE,
33     GR_END,
34     SP_SPARK, SP_SPARKAT, SP_USED, SP_PRUNED, SP_EXPORTED, SP_ACQUIRED, SP_REQUESTED,
35     GR_ALLOC,
36     GR_TERMINATE,
37     GR_SYSTEM_START, GR_SYSTEM_END,            /* only for debugging */
38     GR_EVENT_MAX
39 } GranEventType;
40
41 extern char *gran_event_names[];
42 #endif
43
44 #if defined(GRAN)                                            /* whole file */
45
46 /* Event Types (internal use only) */
47 typedef enum rtsEventType_ {
48  ContinueThread = 0,  /* Continue running the first thread in the queue */
49  StartThread,         /* Start a newly created thread */
50  ResumeThread,        /* Resume a previously running thread */
51  MoveSpark,           /* Move a spark from one PE to another */
52  MoveThread,          /* Move a thread from one PE to another */
53  FindWork,            /* Search for work */
54  FetchNode,           /* Fetch a node */
55  FetchReply,          /* Receive a node */
56  GlobalBlock,         /* Block a TSO on a remote node */
57  UnblockThread        /* Make a TSO runnable */
58 } rtsEventType;
59
60 /* Number of last event type */
61 #define MAX_EVENT       9
62  
63 typedef struct rtsEvent_ {
64   PEs           proc;    /* Processor id */
65   PEs           creator; /* Processor id of PE that created the event */
66   rtsEventType  evttype; /* rtsEvent type */
67   rtsTime       time;    /* Time at which event happened */
68   StgTSO       *tso;     /* Associated TSO, if relevant */
69   StgClosure   *node;    /* Associated node, if relevant */
70   rtsSpark     *spark;   /* Associated SPARK, if relevant */
71   StgInt        gc_info; /* Counter of heap objects to mark (used in GC only)*/
72   struct rtsEvent_ *next;
73   } rtsEvent;
74
75 typedef rtsEvent *rtsEventQ;
76
77 extern rtsEventQ EventHd;
78
79 /* Interface for ADT of Event Queue */
80 rtsEvent *get_next_event(void);
81 rtsTime   get_time_of_next_event(void);
82 void      insert_event(rtsEvent *newentry);
83 void      new_event(PEs proc, PEs creator, rtsTime time, 
84                     rtsEventType evttype, StgTSO *tso, 
85                     StgClosure *node, rtsSpark *spark);
86 void      print_event(rtsEvent *event);
87 void      print_eventq(rtsEvent *hd);
88 void      prepend_event(rtsEvent *event);
89 rtsEventQ grab_event(void);
90 void      prune_eventq(StgTSO *tso, StgClosure *node); 
91
92 void      traverse_eventq_for_gc(void);
93 void      markEventQueue(void);
94
95 //@node Spark handling routines, Processor related stuff, Event queue, Headers for GranSim objs used only in the RTS internally
96 //@subsection Spark handling routines
97
98 /* These functions are only used in the RTS internally; see GranSim.h for rest */
99 void      disposeSpark(rtsSpark *spark);
100 void      disposeSparkQ(rtsSparkQ spark);
101 void      print_spark(rtsSpark *spark);
102 void      print_sparkq(PEs proc);
103 void      print_sparkq_stats(void);
104 nat       spark_queue_len(PEs proc);
105 rtsSpark *delete_from_sparkq (rtsSpark *spark, PEs p, rtsBool dispose_too);
106 void      markSparkQueue(void);
107
108 //@node Processor related stuff, Local types, Spark handling routines, Headers for GranSim objs used only in the RTS internally
109 //@subsection Processor related stuff
110
111 typedef enum rtsProcStatus_ {
112   Idle = 0,             /* empty threadq */
113   Sparking,             /* non-empty sparkq; FINDWORK has been issued */
114   Starting,             /* STARTTHREAD has been issue */
115   Fetching,             /* waiting for remote data (only if block-on-fetch) */
116   Fishing,              /* waiting for remote spark/thread */
117   Busy                  /* non-empty threadq, with head of queue active */
118 } rtsProcStatus;
119
120 /*
121 #define IS_IDLE(proc)        (procStatus[proc] == Idle)
122 #define IS_SPARKING(proc)    (procStatus[proc] == Sparking)
123 #define IS_STARTING(proc)    (procStatus[proc] == Starting)
124 #define IS_FETCHING(proc)    (procStatus[proc] == Fetching)
125 #define IS_FISHING(proc)     (procStatus[proc] == Fishing)
126 #define IS_BUSY(proc)        (procStatus[proc] == Busy)    
127 #define ANY_IDLE             (any_idle())
128 #define MAKE_IDLE(proc)      procStatus[proc] = Idle
129 #define MAKE_SPARKING(proc)  procStatus[proc] = Sparking
130 #define MAKE_STARTING(proc)  procStatus[proc] = Starting
131 #define MAKE_FETCHING(proc)  procStatus[proc] = Fetching
132 #define MAKE_FISHING(proc)   procStatus[proc] = Fishing
133 #define MAKE_BUSY(proc)      procStatus[proc] = Busy
134 */
135
136 //@node Local types, Statistics gathering, Processor related stuff, Headers for GranSim objs used only in the RTS internally
137 //@subsection Local types
138
139 /* Return codes of HandleFetchRequest:
140     0 ... ok (FETCHREPLY event with a buffer containing addresses of the 
141               nearby graph has been scheduled)
142     1 ... node is already local (fetched by somebody else; no event is
143                                   scheduled in here)
144     2 ... fetch request has been forwrded to the PE that now contains the
145            node
146     3 ... node is a black hole (BH, BQ or RBH); no event is scheduled, and
147            the current TSO is put into the blocking queue of that node
148     4 ... out of heap in PackNearbyGraph; GC should be triggered in calling
149           function to guarantee that the tso and node inputs are valid
150           (they may be moved during GC).
151    Return codes of blockFetch:
152     0 ... ok; tso is now at beginning of BQ attached to the bh closure
153     1 ... the bh closure is no BH any more; tso is immediately unblocked
154 */
155
156 typedef enum rtsFetchReturnCode_ {
157   Ok = 0,
158   NodeIsLocal,
159   NodeHasMoved,
160   NodeIsBH,
161   NodeIsNoBH,
162   OutOfHeap,
163 } rtsFetchReturnCode;
164   
165 //@node Statistics gathering, Prototypes, Local types, Headers for GranSim objs used only in the RTS internally
166 //@subsection Statistics gathering
167
168 extern unsigned int /* nat */ OutstandingFetches[], OutstandingFishes[];
169 extern rtsProcStatus procStatus[];
170 extern StgTSO *BlockedOnFetch[];
171
172 /* global structure for collecting statistics */
173 typedef struct GlobalGranStats_ {
174   /* event stats */
175   nat noOfEvents;
176   nat event_counts[MAX_EVENT];
177
178   /* communication stats */
179   nat fetch_misses;
180   nat tot_fake_fetches;   // GranSim internal; faked Fetches are a kludge!!
181   nat tot_low_pri_sparks;
182
183   /* load distribution statistics */  
184   nat rs_sp_count, rs_t_count, ntimes_total, fl_total, 
185       no_of_steals, no_of_migrates;
186
187   /* spark queue stats */
188   nat tot_sq_len, tot_sq_probes, tot_sparks;
189   nat tot_add_threads, tot_tq_len, non_end_add_threads;
190
191   /* packet statistics */
192   nat tot_packets, tot_packet_size, tot_cuts, tot_thunks;
193
194   /* thread stats */
195   nat tot_threads_created, threads_created_on_PE[MAX_PROC],
196       tot_TSOs_migrated;
197
198   /* spark stats */
199   nat pruned_sparks, withered_sparks;
200   nat tot_sparks_created, sparks_created_on_PE[MAX_PROC];
201
202   /* scheduling stats */
203   nat tot_yields, tot_stackover, tot_heapover;
204
205   /* blocking queue statistics */
206   rtsTime tot_bq_processing_time;
207   nat tot_bq_len, tot_bq_len_local, tot_awbq, tot_FMBQs;
208 } GlobalGranStats;
209
210 extern GlobalGranStats globalGranStats;
211
212 //@node Prototypes,  , Statistics gathering, Headers for GranSim objs used only in the RTS internally
213 //@subsection Prototypes
214
215 /* Generally useful fcts */
216 PEs where_is(StgClosure *node);
217 rtsBool is_unique(StgClosure *node);
218
219 /* Prototypes of event handling functions; needed in Schedule.c:ReSchedule() */
220 void do_the_globalblock (rtsEvent* event);
221 void do_the_unblock (rtsEvent* event);
222 void do_the_fetchnode (rtsEvent* event);
223 void do_the_fetchreply (rtsEvent* event);
224 void do_the_movethread (rtsEvent* event);
225 void do_the_movespark (rtsEvent* event);
226 void do_the_startthread(rtsEvent *event);
227 void do_the_findwork(rtsEvent* event);
228 void gimme_spark (rtsEvent *event, rtsBool *found_res, rtsSparkQ *spark_res);
229 rtsBool munch_spark (rtsEvent *event, rtsSparkQ spark);
230
231 /* GranSimLight routines */
232 void GranSimLight_enter_system(rtsEvent *event, StgTSO **ActiveTSOp);
233 void GranSimLight_leave_system(rtsEvent *event, StgTSO **ActiveTSOp);
234
235 /* Communication related routines */
236 rtsFetchReturnCode fetchNode(StgClosure* node, PEs from, PEs to);
237 rtsFetchReturnCode handleFetchRequest(StgClosure* node, PEs curr_proc, PEs p, StgTSO* tso);
238 void               handleIdlePEs(void);
239
240 long int random(void); /* used in stealSpark() and stealThread() in GranSim.c */
241
242 /* Scheduling fcts defined in GranSim.c */
243 void    insertThread(StgTSO *tso, PEs proc);
244 void    endThread(StgTSO *tso, PEs proc);
245 rtsBool GranSimLight_insertThread(StgTSO *tso, PEs proc);
246 nat     thread_queue_len(PEs proc);
247
248 /* For debugging */
249 rtsBool is_on_queue (StgTSO *tso, PEs proc);
250 #endif
251
252 #if defined(GRAN) || defined(PAR)
253 /* 
254    Interface for dumping routines (i.e. writing to log file).
255    These routines are shared with GUM (and could also be used for SMP).
256 */
257 void DumpGranEvent(GranEventType name, StgTSO *tso);
258 void DumpEndEvent(PEs proc, StgTSO *tso, rtsBool mandatory_thread);
259 void DumpTSO(StgTSO *tso);
260 void DumpRawGranEvent(PEs proc, PEs p, GranEventType name, 
261                       StgTSO *tso, StgClosure *node, 
262                       StgInt sparkname, StgInt len);
263 void DumpVeryRawGranEvent(rtsTime time, PEs proc, PEs p, GranEventType name,
264                           StgTSO *tso, StgClosure *node, 
265                           StgInt sparkname, StgInt len);
266 #endif
267
268 #endif /* GRANSIM_RTS_H  */