SST/macro
thread.h
Go to the documentation of this file.
1 /*
2  * This file is part of SST/macroscale:
3  * The macroscale architecture simulator from the SST suite.
4  * Copyright (c) 2009 Sandia Corporation.
5  * This software is distributed under the BSD License.
6  * Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation,
7  * the U.S. Government retains certain rights in this software.
8  * For more information, see the LICENSE file in the top
9  * SST/macroscale directory.
10  */
11 
12 #ifndef SSTMAC_SOFTWARE_PROCESS_THREAD_H_INCLUDED
13 #define SSTMAC_SOFTWARE_PROCESS_THREAD_H_INCLUDED
14 
20 #include <sprockit/errors.h>
21 
32 #include <queue>
33 #include <map>
34 #include <utility>
35 #include <list>
36 
37 #include <unistd.h>
38 
39 namespace sstmac {
40 namespace sw {
41 
42 class thread
43 {
44  public:
45  friend class operating_system;
46  friend class app;
47  friend class delete_thread_event;
48 
49  /// Help resolve deadlock situations.
50  enum state {
53  ACTIVE=2,
57  DONE=6
58  };
59 
60  virtual std::string
61  to_string() const {
62  return "thread";
63  }
64 
65  void
66  set_api(thread* thr) {
67  thr->apis_ = apis_;
68  }
69 
70  static thread*
71  current();
72 
73  template <class T>
74  T*
75  get_api() {
76  api* a = _get_api(T::api_name);
77  T* casted = dynamic_cast<T*>(a);
78  if (!casted) {
80  "Failed to cast API to correct type for %s",
81  T::api_name);
82  }
83  return casted;
84  }
85 
86  virtual app*
87  parent_app() const {
88  return parent_app_;
89  }
90 
91  virtual void
93 
94  static const int no_core_affinity = -1;
95  static const int no_socket_affinity = -1;
96  static const int main_thread = -1;
97  static const int nic_thread = -2;
98  static const int rdma_thread = -3;
99  static const app_id main_thread_aid;
100  static const task_id main_thread_tid;
101 
102 
103  public:
104  virtual
105  ~thread();
106 
107  /// Get current thread state.
108  state
109  get_state() const {
110  return state_;
111  }
112 
113  virtual void
114  init_perf_model_params(sprockit::sim_parameters* params);
115 
116  app_id aid() const {
117  return sid_.app_;
118  }
119 
120  task_id tid() const {
121  return sid_.task_;
122  }
123 
124  void
126  sid_ = sid;
127  }
128 
129  void
130  spawn(thread* thr);
131 
132  long
133  init_id();
134 
135  long
136  thread_id() const {
137  return thread_id_;
138  }
139 
140  /**
141  * This thread is not currently active - blocked on something
142  * However, some kill event happened and I never want to see
143  * this thread again. Make sure the thread doesn't unblock
144  * and clean up all resources associated with the thread
145  */
146  void
148  state_ = CANCELED;
149  }
150 
151  bool
152  is_canceled() const {
153  return state_ == CANCELED;
154  }
155 
156  virtual void
157  kill();
158 
160  os() const {
161  return os_;
162  }
163 
164  app_launch*
165  env() const;
166 
167  void*
168  stack() const {
169  return stack_;
170  }
171 
172  size_t
173  stacksize() const {
174  return stacksize_;
175  }
176 
177  void**
178  backtrace() const {
179  return backtrace_;
180  }
181 
182  int
184  return last_bt_collect_nfxn_;
185  }
186 
187  int
188  backtrace_nfxn() const {
189  return bt_nfxn_;
190  }
191 
192  void
193  append_backtrace(void* fxn);
194 
195  void
196  pop_backtrace();
197 
198  void
199  set_backtrace(void** bt) {
200  backtrace_ = bt;
201  }
202 
203  void collect_backtrace(int nfxn);
204 
205  void
206  init_thread(int phyiscal_thread_id,
207  threading_interface* tocopy, void *stack, int stacksize,
209 
210  /// Derived types need to override this method.
211  virtual void
212  run() = 0;
213 
214  /// A convenience request to start a new thread.
215  /// The current thread has to be initialized for this to work.
216  void
217  start_thread(thread* thr);
218 
219  void
220  join();
221 
224  return p_txt_;
225  }
226 
227  /**
228  * @brief key used
229  * @return
230  */
231  key*
233  return schedule_key_;
234  }
235 
236  /// Test whether the current task has been initialized (activated)
237  /// by a scheduler.
238  bool
239  is_initialized() const {
240  return state_ >= INITIALIZED;
241  }
242 
243  void
244  register_lib(library* lib);
245 
246  void
247  set_affinity(int core){
248  zero_affinity();
249  add_affinity(core);
250  }
251 
252  void
253  add_affinity(int core){
254  cpumask_ = cpumask_ | (1<<core);
255  }
256 
257  void
259  cpumask_ = 0;
260  }
261 
262  template <class T>
263  T&
266  perf_counter_impl<T>* pctr = dynamic_cast<perf_counter_impl<T>*>(ctr);
267  if (!pctr){
269  "failed casting perf_counter type - check perf_model in params");
270  }
271  return pctr->counters();
272  }
273 
274  void
277  }
278 
280  perf_ctr_model() const {
281  return perf_model_;
282  }
283 
284  void
285  set_cpumask(uint64_t cpumask){
286  cpumask_ = cpumask;
287  }
288 
289  uint64_t
290  cpumask() const {
291  return cpumask_;
292  }
293 
294  int
295  active_core() const {
296  return active_core_;
297  }
298 
299  void
300  set_active_core(int core) {
301  active_core_ = core;
302  }
303 
304  typedef spkt_unordered_map<long, thread*> pthread_map_t;
305  void
306  set_pthread_map(pthread_map_t* threadmap){
307  pthread_map_ = threadmap;
308  }
309 
310  void*
311  get_tls_value(long thekey) const;
312 
313  void
314  set_tls_value(long thekey, void* ptr);
315 
316  timestamp
317  now();
318 
319  protected:
320  thread();
321 
322  friend api* static_get_api(const char *name);
323 
324  virtual api*
325  _get_api(const char* name);
326 
327  void
329 
330  private:
331  /// Run routine that defines the initial context for this task.
332  /// This routine calls the virtual thread::run method.
333  static void
334  run_routine(void* threadptr);
335 
336  /**
337  * This should only ever be invoked by the delete thread event.
338  * This ensures that the thread is completely done being operated on
339  * It is now safe to free all resources (thread-local vars, etc)
340  */
341  void cleanup();
342 
343  protected:
344  spkt_unordered_map<std::string, api*> apis_;
345 
346  /// Monitor state for deadlock detection.
348 
349  /// Each thread can only run under one OS/scheduler.
351 
352  std::queue<key*> joiners_;
353 
354  app* parent_app_; // who created this one. null if launch/os.
355 
357 
358  private:
359  bool isInit;
360 
361  void** backtrace_;
362 
363  int bt_nfxn_;
364 
365  std::map<long, void*> tls_values_;
366 
367  pthread_map_t* pthread_map_;
368 
370 
371  /// The stack given to this thread.
372  void* stack_;
373  /// The stacksize.
374  size_t stacksize_;
375 
377 
379 
381 
383 
384  /// This key gets used by the compute scheduler to delay this thread
385  ///
387 
388  std::list<library*> pending_libs_;
389 
390  uint64_t cpumask_;
391 
393 
394 };
395 
396 }
397 } // end of namespace sstmac
398 #endif
uint64_t cpumask_
Definition: thread.h:390
void add_affinity(int core)
Definition: thread.h:253
bool is_canceled() const
Definition: thread.h:152
void set_pthread_map(pthread_map_t *threadmap)
Definition: thread.h:306
virtual app * parent_app() const
Definition: thread.h:87
process_context get_process_context() const
Definition: thread.h:223
virtual void run()=0
Derived types need to override this method.
void set_sid(software_id sid)
Definition: thread.h:125
virtual void init_perf_model_params(sprockit::sim_parameters *params)
void * stack() const
Definition: thread.h:168
static thread * current()
spkt_unordered_map< long, thread * > pthread_map_t
Definition: thread.h:304
static const task_id main_thread_tid
Definition: thread.h:100
static const int no_socket_affinity
Definition: thread.h:95
long thread_id() const
Definition: thread.h:136
void * get_tls_value(long thekey) const
software_id sid_
Definition: thread.h:376
void append_backtrace(void *fxn)
friend api * static_get_api(const char *name)
void ** backtrace() const
Definition: thread.h:178
virtual void remove_variable(void *ptr)=0
perf_counter_model * perf_model_
Definition: thread.h:380
void set_backtrace(void **bt)
Definition: thread.h:199
int backtrace_nfxn() const
Definition: thread.h:188
operating_system * os() const
Definition: thread.h:160
process_context p_txt_
Definition: thread.h:356
void * stack_
The stack given to this thread.
Definition: thread.h:372
app_launch * env() const
The app derived class adds to the thread base class by providing facilities to allow applications to ...
Definition: app.h:55
int task_id
Definition: task_id.h:20
key * schedule_key_
This key gets used by the compute scheduler to delay this thread.
Definition: thread.h:386
app_id aid() const
Definition: thread.h:116
size_t stacksize() const
Definition: thread.h:173
bool is_initialized() const
Test whether the current task has been initialized (activated) by a scheduler.
Definition: thread.h:239
pthread_map_t * pthread_map_
Definition: thread.h:367
virtual void kill()
spkt_unordered_map< std::string, api * > apis_
Definition: thread.h:344
void unregister_all_libs()
static const int main_thread
Definition: thread.h:96
task_id tid() const
Definition: thread.h:120
A basic container for time (subject to future transplant).
Definition: timestamp.h:29
int last_bt_collect_nfxn_
Definition: thread.h:369
SUMI = Simulator unified messagine interface It is also the name for a solid ink in Japanese - i...
T & register_perf_ctr_variable(void *ptr)
Definition: thread.h:264
void set_tls_value(long thekey, void *ptr)
void cancel()
This thread is not currently active - blocked on something However, some kill event happened and I ne...
Definition: thread.h:147
void zero_affinity()
Definition: thread.h:258
size_t stacksize_
The stacksize.
Definition: thread.h:374
state
Help resolve deadlock situations.
Definition: thread.h:50
state state_
Monitor state for deadlock detection.
Definition: thread.h:347
friend class delete_thread_event
Definition: thread.h:47
void set_cpumask(uint64_t cpumask)
Definition: thread.h:285
A base type and default (empty) implementation of a handle to block and unblock processes.
Definition: key.h:30
std::queue< key * > joiners_
Definition: thread.h:352
virtual perf_counter * register_variable(void *ptr)=0
std::map< long, void * > tls_values_
Definition: thread.h:365
app * parent_app_
Definition: thread.h:354
void ** backtrace_
Definition: thread.h:361
perf_counter_model * perf_ctr_model() const
Definition: thread.h:280
uint64_t cpumask() const
Definition: thread.h:290
#define spkt_throw_printf(exc, template_str,...)
Definition: errors.h:37
void register_lib(library *lib)
void set_affinity(int core)
Definition: thread.h:247
static const app_id main_thread_aid
Definition: thread.h:99
void cleanup()
This should only ever be invoked by the delete thread event.
key * schedule_key()
key used
Definition: thread.h:232
static const int no_core_affinity
Definition: thread.h:94
void collect_backtrace(int nfxn)
virtual std::string to_string() const
Definition: thread.h:61
threading_interface * context_
Definition: thread.h:382
#define spkt_throw(exc,...)
Definition: errors.h:44
std::list< library * > pending_libs_
Definition: thread.h:388
operating_system * os_
Each thread can only run under one OS/scheduler.
Definition: thread.h:350
int active_core() const
Definition: thread.h:295
virtual void clear_subthread_from_parent_app()
static const int rdma_thread
Definition: thread.h:98
int last_backtrace_nfxn() const
Definition: thread.h:183
void start_thread(thread *thr)
A convenience request to start a new thread.
void spawn(thread *thr)
void init_thread(int phyiscal_thread_id, threading_interface *tocopy, void *stack, int stacksize, operating_system *os, threading_interface *yield_to)
static void run_routine(void *threadptr)
Run routine that defines the initial context for this task.
static const int nic_thread
Definition: thread.h:97
void set_api(thread *thr)
Definition: thread.h:66
void set_active_core(int core)
Definition: thread.h:300
void remove_perf_ctr_variable(void *ptr)
Definition: thread.h:275
state get_state() const
Get current thread state.
Definition: thread.h:109
A wrapper for an appid, taskid pair.
Definition: software_id.h:28
int app_id
Definition: app_id.h:20
virtual api * _get_api(const char *name)
Error indicating some internal value was unexpected.
Definition: errors.h:83