Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: cedb24bf17e4ad96a09111840b7d4b30575d09da (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
# *****************************************************************************
# * Copyright (c) 2011, 2013 Wind River Systems, Inc. and others.
# * All rights reserved. This program and the accompanying materials
# * are made available under the terms of the Eclipse Public License v1.0
# * which accompanies this distribution, and is available at
# * http://www.eclipse.org/legal/epl-v10.html
# *
# * Contributors:
# *     Wind River Systems - initial API and implementation
# *****************************************************************************

"""This is an optional service that can be implemented by a peer.

If implemented, the service can be used for monitoring system activity and
utilization.

It provides list of running processes, different process attributes like
command line, environment, etc., and some resource utilization data.

The service can be used by a client to provide functionality similar to Unix
**top** utility or Windows **Task Manager**.

.. |getChildren| replace:: :meth:`~SysMonitorService.getChildren`
.. |getCommandLine| replace:: :meth:`~SysMonitorService.getCommandLine`
.. |getContext| replace:: :meth:`~SysMonitorService.getContext`
.. |getEnvironment| replace:: :meth:`~SysMonitorService.getEnvironment`
.. |getProperties| replace:: :meth:`SysMonitorContext.getProperties`
.. |DoneGetChildren| replace:: :class:`DoneGetChildren`
.. |DoneGetCommandLine| replace:: :class:`DoneGetCommandLine`
.. |DoneGetContext| replace:: :class:`DoneGetContext`
.. |DoneGetEnvironment| replace:: :class:`DoneGetEnvironment`
.. |SysMonitorContext| replace:: :class:`SysMonitorContext`

Properties
----------
System Monitor Context Properties
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
.. seealso:: |SysMonitorContext|, |getProperties|

+------------------+--------------+-------------------------------------------+
| Name             | Type         | Description                               |
+==================+==============+===========================================+
| PROP_CMAJFLT     | |int|        | The number of major faults that the       |
|                  |              | process's  waited-for  children have made.|
+------------------+--------------+-------------------------------------------+
| PROP_CMINFLT     | |int|        | The number of minor faults that the       |
|                  |              | process's waited-for  children have made. |
+------------------+--------------+-------------------------------------------+
| PROP_CNSWAP      | |int|        | Cumulative NSwap for child processes.     |
+------------------+--------------+-------------------------------------------+
| PROP_CODEEND     | |int|        | The address below which program text can  |
|                  |              | run.                                      |
+------------------+--------------+-------------------------------------------+
| PROP_CODESTART   | |int|        | The address above which program text can  |
|                  |              | run.                                      |
+------------------+--------------+-------------------------------------------+
| PROP_CSTIME      | |int|        | The number of jiffies that this process's |
|                  |              | waited-for children have been scheduled in|
|                  |              | user mode.                                |
+------------------+--------------+-------------------------------------------+
| PROP_CUTIME      | |int|        | The number of jiffies that this process's |
|                  |              | waited-for children have been scheduled in|
|                  |              | user mode.                                |
+------------------+--------------+-------------------------------------------+
| PROP_CWD         | |basestring| | Current working directory of the process. |
+------------------+--------------+-------------------------------------------+
| PROP_EXITSIGNAL  | |int|        | Signal to be sent to parent when this     |
|                  |              | process exits.                            |
+------------------+--------------+-------------------------------------------+
| PROP_FILE        | |basestring| | Executable file of the process.           |
+------------------+--------------+-------------------------------------------+
| PROP_FLAGS       | |int|        | The kernel flags word of the process.     |
|                  |              | Details depend on the kernel.             |
+------------------+--------------+-------------------------------------------+
| PROP_GROUPNAME   | |basestring| | Group name of the process owner.          |
+------------------+--------------+-------------------------------------------+
| PROP_ID          | |basestring| | The TCF context ID.                       |
+------------------+--------------+-------------------------------------------+
| PROP_ITREALVALUE | |int|        | The time in milliseconds before the next  |
|                  |              | ``SIGALRM`` is sent  to  the  process due |
|                  |              | to an interval timer.                     |
+------------------+--------------+-------------------------------------------+
| PROP_MAJFLT      | |int|        | The number of major faults the process has|
|                  |              | made which have required loading a memory |
|                  |              | page from disk.                           |
+------------------+--------------+-------------------------------------------+
| PROP_MINFLT      | |int|        | The number of minor faults the process has|
|                  |              | made which have not required loading a    |
|                  |              | memory page from disk.                    |
+------------------+--------------+-------------------------------------------+
| PROP_NICE        | |int|        | The nice value.                           |
+------------------+--------------+-------------------------------------------+
| PROP_NSWAP       | |int|        | Number of pages swapped.                  |
+------------------+--------------+-------------------------------------------+
| PROP_PARENTID    | |basestring| | The TCF parent context ID.                |
+------------------+--------------+-------------------------------------------+
| PROP_PGRP        | |int|        | The process group ID of the process.      |
+------------------+--------------+-------------------------------------------+
| PROP_PID         | |int|        | System process ID.                        |
+------------------+--------------+-------------------------------------------+
| PROP_POLICY      | |basestring| | Scheduling policy.                        |
+------------------+--------------+-------------------------------------------+
| PROP_PPID        | |int|        | System ID of the parent process.          |
+------------------+--------------+-------------------------------------------+
| PROP_PRIORITY    | |int|        | The standard nice value.                  |
+------------------+--------------+-------------------------------------------+
| PROP_PROCESSOR   | |int|        | CPU number last executed on.              |
+------------------+--------------+-------------------------------------------+
| PROP_PSIZE       | |int|        | Memory pages size in bytes.               |
+------------------+--------------+-------------------------------------------+
| PROP_RLIMIT      | |int|        | Current limit in bytes on the rss of the  |
|                  |              | process.                                  |
+------------------+--------------+-------------------------------------------+
| PROP_ROOT        | |basestring| | The process's root directory (as set by   |
|                  |              | **chroot**).                              |
+------------------+--------------+-------------------------------------------+
| PROP_RSS         | |int|        | Resident Set Size: number of pages the    |
|                  |              | process has in real memory, minus used for|
|                  |              | administrative purposes. This is just the |
|                  |              | pages which count towards text, data, or  |
|                  |              | stack space. This does not include pages  |
|                  |              | which have not been demand-loaded in, or  |
|                  |              | which are swapped out.                    |
+------------------+--------------+-------------------------------------------+
| PROP_RTPRIORITY  | |int|        | Real-time scheduling priority.            |
+------------------+--------------+-------------------------------------------+
| PROP_SESSION     | |int|        | The session ID of the process.            |
+------------------+--------------+-------------------------------------------+
| PROP_SIGBLOCK    | |int|        | The bitmap of blocked signals.            |
+------------------+--------------+-------------------------------------------+
| PROP_SIGCATCH    | |int|        | The bitmap of caught signals.             |
+------------------+--------------+-------------------------------------------+
| PROP_SIGIGNORE   | |int|        | The bitmap of ignored signals.            |
+------------------+--------------+-------------------------------------------+
| PROP_SIGNALS     | |int|        | The bitmap of pending signals.            |
+------------------+--------------+-------------------------------------------+
| PROP_STACKSTART  | |int|        | The address of the start of the stack.    |
+------------------+--------------+-------------------------------------------+
| PROP_STARTTIME   | |int|        | The time in milliseconds the process      |
|                  |              | started after system boot.                |
+------------------+--------------+-------------------------------------------+
| PROP_STATE       | |basestring| | One character from the string ``RSDZTW``  |
|                  |              | where :                                   |
|                  |              |                                           |
|                  |              |     - **R** is running.                   |
|                  |              |     - **S** is sleeping in an             |
|                  |              |       interruptible wait.                 |
|                  |              |     - **D** is waiting in uninterruptible |
|                  |              |       disk sleep.                         |
|                  |              |     - **Z** is zombie.                    |
|                  |              |     - **T** is traced or stopped (on a    |
|                  |              |       signal).                            |
|                  |              |     - **W** is paging.                    |
+------------------+--------------+-------------------------------------------+
| PROP_STIME       | |int|        | The number of milliseconds that this      |
|                  |              | process has been scheduled in kernel mode.|
+------------------+--------------+-------------------------------------------+
| PROP_TGID        | |int|        | The process group ID of the process which |
|                  |              | currently owns the tty that the process is|
|                  |              | connected to.                             |
+------------------+--------------+-------------------------------------------+
| PROP_TRACERPID   | |int|        | ID of a process that has attached this    |
|                  |              | process for tracing or debugging.         |
+------------------+--------------+-------------------------------------------+
| PROP_TTY         | |int|        | The tty the process uses.                 |
+------------------+--------------+-------------------------------------------+
| PROP_UGID        | |int|        | Group ID of the process owner.            |
+------------------+--------------+-------------------------------------------+
| PROP_UID         | |int|        | User ID of the process owner.             |
+------------------+--------------+-------------------------------------------+
| PROP_USERNAME    | |basestring| | User name of the process owner.           |
+------------------+--------------+-------------------------------------------+
| PROP_UTIME       | |int|        | The number of milliseconds that this      |
|                  |              | process has been scheduled in user mode.  |
+------------------+--------------+-------------------------------------------+
| PROP_VSIZE       | |int|        | Virtual memory size in bytes.             |
+------------------+--------------+-------------------------------------------+
| PROP_WCHAN       | |int|        | This is the *channel* in which the process|
|                  |              | is waiting. It is the address of a system |
|                  |              | call, and can be looked up in a name list |
|                  |              | if you need a textual name.               |
+------------------+--------------+-------------------------------------------+

Service Methods
---------------
.. autodata:: NAME
.. autoclass:: SysMonitorService

getChildren
^^^^^^^^^^^
.. automethod:: SysMonitorService.getChildren

getCommandLine
^^^^^^^^^^^^^^
.. automethod:: SysMonitorService.getCommandLine

getContext
^^^^^^^^^^
.. automethod:: SysMonitorService.getContext

getEnvironment
^^^^^^^^^^^^^^
.. automethod:: SysMonitorService.getEnvironment

getName
^^^^^^^
.. automethod:: SysMonitorService.getName

Callback Classes
----------------
DoneGetChildren
^^^^^^^^^^^^^^^
.. autoclass:: DoneGetChildren
    :members:

DoneGetCommandLine
^^^^^^^^^^^^^^^^^^
.. autoclass:: DoneGetCommandLine
    :members:

DoneGetContext
^^^^^^^^^^^^^^
.. autoclass:: DoneGetContext
    :members:

DoneGetEnvironment
^^^^^^^^^^^^^^^^^^
.. autoclass:: DoneGetEnvironment
    :members:

Helper Classes
--------------
SysMonitorContext
^^^^^^^^^^^^^^^^^
.. autoclass:: SysMonitorContext
    :members:
"""

from .. import services

NAME = "SysMonitor"
"""SysMonitor service name."""

# Context property names.
PROP_ID = "ID"
PROP_PARENTID = "ParentID"
PROP_CWD = "CWD"
PROP_ROOT = "Root"
PROP_UID = "UID"
PROP_UGID = "UGID"
PROP_USERNAME = "UserName"
PROP_GROUPNAME = "GroupName"
PROP_PID = "PID"
PROP_FILE = "File"
PROP_STATE = "State"
PROP_PPID = "PPID"
PROP_PGRP = "PGRP"
PROP_SESSION = "Session"
PROP_TTY = "TTY"
PROP_TGID = "TGID"
PROP_TRACERPID = "TracerPID"
PROP_FLAGS = "Flags"
PROP_MINFLT = "MinFlt"
PROP_CMINFLT = "CMinFlt"
PROP_MAJFLT = "MajFlt"
PROP_CMAJFLT = "CMajFlt"
PROP_UTIME = "UTime"
PROP_STIME = "STime"
PROP_CUTIME = "CUTime"
PROP_CSTIME = "CSTime"
PROP_PRIORITY = "Priority"
PROP_NICE = "Nice"
PROP_ITREALVALUE = "ITRealValue"
PROP_STARTTIME = "StartTime"
PROP_VSIZE = "VSize"
PROP_PSIZE = "PSize"
PROP_RSS = "RSS"
PROP_RLIMIT = "RLimit"
PROP_CODESTART = "CodeStart"
PROP_CODEEND = "CodeEnd"
PROP_STACKSTART = "StackStart"
PROP_SIGNALS = "Signals"
PROP_SIGBLOCK = "SigBlock"
PROP_SIGIGNORE = "SigIgnore"
PROP_SIGCATCH = "SigCatch"
PROP_WCHAN = "WChan"
PROP_NSWAP = "NSwap"
PROP_CNSWAP = "CNSwap"
PROP_EXITSIGNAL = "ExitSignal"
PROP_PROCESSOR = "Processor"
PROP_RTPRIORITY = "RTPriority"
PROP_POLICY = "Policy"


class SysMonitorContext(object):
    """A context corresponds to an execution thread, process, address space,
    etc.

    A context can belong to a parent context. Contexts hierarchy can be simple
    plain list or it can form a tree. It is up to target agent developers to
    choose layout that is most descriptive for a given target. Context IDs are
    valid across all services. In other words, all services access same
    hierarchy of contexts, with same IDs, however, each service accesses its
    own subset of context's attributes and functionality, which is relevant to
    that service.

    :param props: The properties to initialise this system monitor context
                  with. See `System Monitor Context Properties`_.
    :type props: |dict|
    """
    def __init__(self, props):
        self._props = props or {}

    def __str__(self):
        return "[Sys Monitor Context %s]" % self._props

    def getID(self):
        """Get TCF context ID.

        :returns: A |basestring| representing this context TCF ID.
        """
        return self._props.get(PROP_ID, '')

    def getParentID(self):
        """Get parent context ID.

        :returns: A |basestring| representing this context parent TCF ID.
        """
        return self._props.get(PROP_PARENTID, '')

    def getPGRP(self):
        """Get process group ID.

        :returns: An |int| representing this context process group ID, or
                  **-1** if it is not known.
        """
        return self._props.get(PROP_PGRP, -1)

    def getPID(self):
        """Get process ID.

        :returns: An |int| representing this context process ID, or
                  **-1** if it is not known.
        """
        return self._props.get(PROP_PID, -1)

    def getPPID(self):
        """Get process parent ID.

        :returns: An |int| representing this context parent process ID, or
                  **-1** if it is not known.
        """
        return self._props.get(PROP_PPID, -1)

    def getTGID(self):
        """Get process TTY group ID.

        :returns: An |int| representing this context process TTY group ID, or
                  **-1** if it is not known.
        """
        return self._props.get(PROP_TGID, -1)

    def getTracerPID(self):
        """Get tracer process ID.

        :returns: An |int| representing this context tracer process ID, or
                  **-1** if it is not known.
        """
        return self._props.get(PROP_TRACERPID, -1)

    def getUID(self):
        """Get process owner user ID.

        :returns: An |int| representing this context process owner user ID, or
                  **-1** if it is not known.
        """
        return self._props.get(PROP_UID, -1)

    def getUserName(self):
        """Get process owner user name.

        :returns: A |basestring| representing this context process owner user
                  name, or an empty string if it is not known.
        """
        return self._props.get(PROP_USERNAME, '')

    def getUGID(self):
        """Get process owner user group ID.

        :returns: An |int| representing this context process owner user group
                  ID, or **-1** if it is not known.
        """
        return self._props.get(PROP_UGID, -1)

    def getGroupName(self):
        """Get process owner user group name.

        :returns: A |basestring| representing this context process owner user
                  group name, or an empty string if it is not known.
        """
        return self._props.get(PROP_GROUPNAME, '')

    def getState(self):
        """Get process state.

        :returns: A |basestring| representing this context process state, or
                  an empty string if it is not known.
        """
        return self._props.get(PROP_STATE, '')

    def getVSize(self):
        """Get process virtual memory size in bytes.

        :returns: An |int| representing this context process virtual memory
                  size in bytes, or **-1** if it is not known.
        """
        return self._props.get(PROP_VSIZE, -1)

    def getPSize(self):
        """Get process virtual memory page size in bytes.

        :returns: An |int| representing this context process virtual memory
                  page size in bytes, or **-1** if it is not known.
        """
        return self._props.get(PROP_PSIZE, -1)

    def getRSS(self):
        """Get number of memory pages in process resident set.

        :returns: An |int| representing this context number of memory pages in
                  process resident set, or **-1** if it is not known.
        """
        return self._props.get(PROP_RSS, -1)

    def getFile(self):
        """Get context executable file.

        :returns: A |basestring| representing this context executable file, or
                  an empty string if it is not known.
        """
        return self._props.get(PROP_FILE, '')

    def getRoot(self):
        """Get context current file system root.

        :returns: A |basestring| representing this context current file system
                  root, or an empty string if it is not known.
        """
        return self._props.get(PROP_ROOT, '')

    def getCurrentWorkingDirectory(self):
        """Get context current working directory.

        :returns: A |basestring| representing this context current working
                  directory, or an empty string if it is not known.
        """
        return self._props.get(PROP_CWD, '')

    def getProperties(self):
        """Get all available context properties.

        :returns: A |dict| of properties definit this system monitor context.

        .. seealso:: `System Monitor Context Properties`_
        """
        return self._props


class SysMonitorService(services.Service):
    """System monitor service interface."""

    def getName(self):
        """Get this service name.

        :returns: A |basestring| representing this service name, which is the
                  value of :const:`NAME`
        """
        return NAME

    def getContext(self, contextID, done):
        """Retrieve context info for given context ID.

        :param contextID: TCF context ID.
        :type contextID: |basestring|
        :param done: Callback interface called when operation is completed.
        :type done: |DoneGetContext|

        :returns: Pending command handle, can be used to cancel the command.
        """
        raise NotImplementedError("Abstract method")

    def getChildren(self, parent_context_id, done):
        """Retrieve children of given context.

        :param parent_context_id: parent context ID. Can be **None** - to
                                  retrieve top level of the hierarchy, or one
                                  of context IDs retrieved by previous
                                  |getContext| or |getChildren| methods.
        :type parent_context_id: |basestring| or **None**
        :param done: callback interface called when operation is completed
        :type done: |DoneGetChildren|

        :returns: Pending command handle, can be used to cancel the command.
        """
        raise NotImplementedError("Abstract method")

    def getCommandLine(self, contextID, done):
        """Get context command line.

        :param contextID: ID of the context to get command line for.
        :type contextID: |basestring|
        :param done: Callback interface called when operation is completed.
        :type done: |DoneGetCommandLine|

        :returns: Pending command handle, can be used to cancel the command.
        """
        raise NotImplementedError("Abstract method")

    def getEnvironment(self, contextID, done):
        """Get context environment variables.

        :param contextID: ID of the context to get environment for.
        :type contextID: |basestring|
        :param done: Callback interface called when operation is completed.
        :type done: |DoneGetEnvironment|

        :returns: Pending command handle, can be used to cancel the command.
        """
        raise NotImplementedError("Abstract method")


class DoneGetContext(object):
    """Client callback interface for |getContext|."""

    def doneGetContext(self, token, error, context):
        """Called when context data retrieval is done.

        :param token: Pending command handle.
        :param error: Error description if operation failed, **None** if
                      succeeded.
        :param context: Retrieved context data.
        :type context: |SysMonitorContext|
        """
        pass


class DoneGetChildren(object):
    """Client callback interface for |getChildren|."""

    def doneGetChildren(self, token, error, context_ids):
        """Called when context list retrieval is done.

        :param token: Pending command handle.
        :param error: Error description if operation failed, **None** if
                      succeeded.
        :param context_ids: A list of available context IDs.
        :type context_ids: |list|
        """
        pass


class DoneGetCommandLine(object):
    """Client callback interface for |getCommandLine|."""

    def doneGetCommandLine(self, token, error, cmd_line):
        """Called when command line retrieval is done.

        :param token: Pending command handle.
        :param error: Error description if operation failed, **None** if
                      succeeded.
        :param cmd_line: A list of |basestring| representing this context
                         command line.
        :type cmd_line: |list|
        """
        pass


class DoneGetEnvironment(object):
    """Client callback interface for |getEnvironment|."""

    def doneGetEnvironment(self, token, error, environment):
        """Called when environment retrieval is done.

        :param token: Pending command handle.
        :param error: Error description if operation failed, **None** if
                      succeeded.
        :param environment: A |dict| of context environment variables.
        :type environment: |dict|
        """
        pass

Back to the top