The Pedigree Project  0.1
Process.h
1 /*
2  * Copyright (c) 2008-2014, Pedigree Developers
3  *
4  * Please see the CONTRIB file in the root of the source tree for a full
5  * list of contributors.
6  *
7  * Permission to use, copy, modify, and distribute this software for any
8  * purpose with or without fee is hereby granted, provided that the above
9  * copyright notice and this permission notice appear in all copies.
10  *
11  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18  */
19 
20 #ifndef PROCESS_H
21 #define PROCESS_H
22 
23 #ifdef THREADS
24 
25 #include "pedigree/kernel/Atomic.h"
26 #include "pedigree/kernel/Spinlock.h"
27 #include "pedigree/kernel/compiler.h"
28 #include "pedigree/kernel/process/Semaphore.h"
29 #include "pedigree/kernel/process/Thread.h"
30 #include "pedigree/kernel/processor/types.h"
31 #include "pedigree/kernel/time/Time.h"
32 #include "pedigree/kernel/utilities/List.h"
33 #include "pedigree/kernel/utilities/MemoryAllocator.h"
34 #include "pedigree/kernel/utilities/StaticString.h"
35 #include "pedigree/kernel/utilities/Vector.h"
36 #include "pedigree/kernel/utilities/new"
37 
38 #include "pedigree/kernel/Subsystem.h"
39 
41 class File;
42 class User;
43 class Group;
44 class DynamicLinker;
45 
51 {
52  friend class Thread;
53 
54  public:
62  {
63  Stock,
64  Posix
65  };
66 
73  {
74  Active,
75  Suspended,
76  Terminating,
77  Terminated,
78  Reaped,
79  };
80 
82  Process();
83 
91  Process(Process *pParent, bool bCopyOnWrite = true);
92 
94  virtual ~Process();
95 
98  size_t addThread(Thread *pThread);
100  void removeThread(Thread *pThread);
101 
103  size_t getNumThreads();
105  Thread *getThread(size_t n);
106 
108  size_t getId()
109  {
110  return m_Id;
111  }
112 
115  {
116  return str;
117  }
118 
121  {
122  return m_pAddressSpace;
123  }
124 
126  void setExitStatus(int code)
127  {
128  m_ExitStatus = code;
129  }
132  {
133  return m_ExitStatus;
134  }
135 
137  void reap()
138  {
139  m_State = Reaped;
140  }
141 
143  void kill() NORETURN;
145  void suspend();
147  void resume();
148 
151  {
152  return m_pParent;
153  }
154 
157  {
158  return m_Cwd;
159  }
161  void setCwd(File *f)
162  {
163  m_Cwd = f;
164  }
165 
168  {
169  return m_Ctty;
170  }
172  void setCtty(File *f)
173  {
174  m_Ctty = f;
175  }
176 
179  {
180  return m_SpaceAllocator;
181  }
184  {
185  return m_DynamicSpaceAllocator;
186  }
187 
189  User *getUser() const
190  {
191  return m_pUser;
192  }
194  void setUser(User *pUser)
195  {
196  m_pUser = pUser;
197  }
198 
201  {
202  return m_pEffectiveUser;
203  }
205  void setEffectiveUser(User *pUser)
206  {
207  m_pEffectiveUser = pUser;
208  }
209 
211  Group *getGroup() const
212  {
213  return m_pGroup;
214  }
216  void setGroup(Group *pGroup)
217  {
218  m_pGroup = pGroup;
219  }
220 
223  {
224  return m_pEffectiveGroup;
225  }
226  void setEffectiveGroup(Group *pGroup)
227  {
228  m_pEffectiveGroup = pGroup;
229  }
230 
233  virtual int64_t getUserId() const;
234  virtual int64_t getGroupId() const;
235  virtual int64_t getEffectiveUserId() const;
236  virtual int64_t getEffectiveGroupId() const;
237  virtual void getSupplementalGroupIds(Vector<int64_t> &vec) const;
238 
239  void setLinker(DynamicLinker *pDl)
240  {
241  m_pDynamicLinker = pDl;
242  }
243  DynamicLinker *getLinker()
244  {
245  return m_pDynamicLinker;
246  }
247 
248  void setSubsystem(Subsystem *pSubsystem)
249  {
250  m_pSubsystem = pSubsystem;
251  m_pSubsystem->setProcess(this);
252  }
253  Subsystem *getSubsystem()
254  {
255  return m_pSubsystem;
256  }
257 
260  {
261  return Stock;
262  }
263 
264  void addWaiter(Semaphore *pWaiter);
265  void removeWaiter(Semaphore *pWaiter);
266  size_t waiterCount() const;
267 
268  bool hasSuspended()
269  {
270  bool bRet = m_bUnreportedSuspend;
271  m_bUnreportedSuspend = false;
272  return bRet;
273  }
274  bool hasResumed()
275  {
276  bool bRet = m_bUnreportedResume;
277  m_bUnreportedResume = false;
278  return bRet;
279  }
280 
281  ProcessState getState() const
282  {
283  return m_State;
284  }
285 
286  void markTerminating()
287  {
288  m_State = Terminating;
289  }
290 
291  void trackHeap(ssize_t nBytes)
292  {
293  m_Metadata.heapUsage += nBytes;
294  }
295 
296  void trackPages(ssize_t nVirtual, ssize_t nPhysical, ssize_t nShared)
297  {
298  m_Metadata.virtualPages += nVirtual;
299  m_Metadata.physicalPages += nPhysical;
300  m_Metadata.sharedPages += nShared;
301  }
302 
303  void resetCounts()
304  {
305  m_Metadata.virtualPages = 0;
306  m_Metadata.physicalPages = 0;
307  m_Metadata.sharedPages = 0;
308  m_Metadata.startTime = Time::getTimeNanoseconds();
309  }
310 
317  void recordTime(bool bUserspace)
318  {
319  Time::Timestamp now = Time::getTimeNanoseconds();
320  if (bUserspace)
321  {
322  m_LastUserspaceEntry = now;
323  }
324  else
325  {
326  m_LastKernelEntry = now;
327  }
328  }
329 
336  void trackTime(bool bUserspace)
337  {
338  Time::Timestamp now = Time::getTimeNanoseconds();
339  if (bUserspace)
340  {
341  Time::Timestamp diff = now - m_LastUserspaceEntry;
342  m_LastUserspaceEntry = now;
343  m_Metadata.userTime += diff;
344 
345  reportTimesUpdated(diff, 0);
346  }
347  else
348  {
349  Time::Timestamp diff = now - m_LastKernelEntry;
350  m_LastKernelEntry = now;
351  m_Metadata.kernelTime += diff;
352 
353  reportTimesUpdated(0, diff);
354  }
355  }
356 
358  Time::Timestamp getUserTime() const
359  {
360  return m_Metadata.userTime;
361  }
362  Time::Timestamp getKernelTime() const
363  {
364  return m_Metadata.kernelTime;
365  }
366  Time::Timestamp getStartTime() const
367  {
368  return m_Metadata.startTime;
369  }
370 
372  ssize_t getHeapUsage() const
373  {
374  return m_Metadata.heapUsage;
375  }
376  ssize_t getVirtualPageCount() const
377  {
378  return m_Metadata.virtualPages;
379  }
380  ssize_t getPhysicalPageCount() const
381  {
382  return m_Metadata.physicalPages;
383  }
384  ssize_t getSharedPageCount() const
385  {
386  return m_Metadata.sharedPages;
387  }
388 
390  void setRootFile(File *pFile)
391  {
392  m_pRootFile = pFile;
393  }
394 
396  File *getRootFile() const
397  {
398  return m_pRootFile;
399  }
400 
406  {
407  return m_bSharedAddressSpace;
408  }
409 
414  static Process *getInit();
415 
417  static void setInit(Process *pProcess);
418 
419  private:
420  Process(const Process &);
421  Process &operator=(const Process &);
422 
424  virtual void
425  reportTimesUpdated(Time::Timestamp user, Time::Timestamp system)
426  {
427  }
428 
430  virtual void processTerminated()
431  {
432  }
433 
445  size_t m_Id;
486 
489 
492 
495 
498 
501 
504 
510 
513 
515  void notifyWaiters();
516 
519  {
521  : virtualPages(0), physicalPages(0), sharedPages(0), userTime(0),
522  kernelTime(0), startTime(0)
523  {
524  }
525 
527  ssize_t heapUsage;
530  ssize_t virtualPages;
532  ssize_t physicalPages;
534  ssize_t sharedPages;
535 
537  Time::Timestamp userTime;
539  Time::Timestamp kernelTime;
540 
542  Time::Timestamp startTime;
543  } m_Metadata;
544 
546  Time::Timestamp m_LastKernelEntry;
547 
549  Time::Timestamp m_LastUserspaceEntry;
550 
553 
556 
559 
560  public:
561  Semaphore m_DeadThreads;
562 };
563 
564 #endif
565 
566 #endif
ProcessType
Definition: Process.h:61
Group * getEffectiveGroup() const
Definition: Process.h:222
bool m_bUnreportedSuspend
Definition: Process.h:497
Spinlock m_Lock
Definition: Process.h:512
virtual void processTerminated()
Definition: Process.h:430
size_t m_Id
Definition: Process.h:445
List< Semaphore * > m_Waiters
Definition: Process.h:494
ssize_t getHeapUsage() const
Definition: Process.h:372
Time::Timestamp m_LastKernelEntry
Definition: Process.h:546
ProcessState m_State
Definition: Process.h:503
size_t getId()
Definition: Process.h:108
int getExitStatus()
Definition: Process.h:131
File * getRootFile() const
Definition: Process.h:396
File * m_pRootFile
Definition: Process.h:552
bool m_bSharedAddressSpace
Definition: Process.h:555
File * m_Ctty
Definition: Process.h:469
Time::Timestamp startTime
Time at which process started.
Definition: Process.h:542
bool hasSharedAddressSpace() const
Definition: Process.h:405
Time::Timestamp kernelTime
Time spent in the kernel as this process.
Definition: Process.h:539
MemoryAllocator m_DynamicSpaceAllocator
Definition: Process.h:477
Subsystem * m_pSubsystem
Definition: Process.h:491
VirtualAddressSpace * m_pAddressSpace
Definition: Process.h:457
Definition: User.h:32
Time::Timestamp m_LastUserspaceEntry
Definition: Process.h:549
void setCtty(File *f)
Definition: Process.h:172
Group * m_pGroup
Definition: Process.h:481
Time::Timestamp userTime
Time spent in userspace as this process.
Definition: Process.h:537
void setEffectiveUser(User *pUser)
Definition: Process.h:205
Vector< Thread * > m_Threads
Definition: Process.h:437
Atomic< size_t > m_NextTid
Definition: Process.h:441
ssize_t sharedPages
Shared pages consumed.
Definition: Process.h:534
File * m_Cwd
Definition: Process.h:465
Process * m_pParent
Definition: Thread.h:502
Process * getParent()
Definition: Process.h:150
MemoryAllocator & getDynamicSpaceAllocator()
Definition: Process.h:183
Time::Timestamp getUserTime() const
Definition: Process.h:358
void reap()
Definition: Process.h:137
Status
Definition: Thread.h:62
Group * m_pEffectiveGroup
Definition: Process.h:485
void setExitStatus(int code)
Definition: Process.h:126
File * getCtty()
Definition: Process.h:167
void recordTime(bool bUserspace)
Definition: Process.h:317
void setRootFile(File *pFile)
Definition: Process.h:390
static Process * m_pInitProcess
Definition: Process.h:558
User * m_pEffectiveUser
Definition: Process.h:483
Group * getGroup() const
Definition: Process.h:211
User * getUser() const
Definition: Process.h:189
void setCwd(File *f)
Definition: Process.h:161
Definition: Thread.h:54
DynamicLinker * m_pDynamicLinker
Definition: Process.h:488
virtual void reportTimesUpdated(Time::Timestamp user, Time::Timestamp system)
Definition: Process.h:425
bool m_bUnreportedResume
Definition: Process.h:500
VirtualAddressSpace * getAddressSpace()
Definition: Process.h:120
LargeStaticString & description()
Definition: Process.h:114
LargeStaticString str
Definition: Process.h:449
virtual ProcessType getType()
Definition: Process.h:259
MemoryAllocator m_SpaceAllocator
Definition: Process.h:473
void trackTime(bool bUserspace)
Definition: Process.h:336
Process * m_pParent
Definition: Process.h:453
size_t m_Id
Definition: Thread.h:510
void setUser(User *pUser)
Definition: Process.h:194
User * getEffectiveUser() const
Definition: Process.h:200
void setGroup(Group *pGroup)
Definition: Process.h:216
Thread & operator=(const Thread &)
Definition: File.h:66
int m_ExitStatus
Definition: Process.h:461
User * m_pUser
Definition: Process.h:479
virtual void setProcess(Process *p)
Definition: Subsystem.h:137
Thread::Status m_BeforeSuspendState
Definition: Process.h:509
File * getCwd()
Definition: Process.h:156
Definition: Group.h:32
MemoryAllocator & getSpaceAllocator()
Definition: Process.h:178
ssize_t heapUsage
Bytes used in the kernel heap by this process.
Definition: Process.h:527
ssize_t physicalPages
Physical address space consumed, barring that which is shared.
Definition: Process.h:532
ProcessState
Definition: Process.h:72