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