summaryrefslogtreecommitdiff
path: root/libps/host.c
blob: 9a46e00867d0c2accf11a51b97b32be18680b3ed (plain)
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
/* Routines to get global host info.

   Copyright (C) 1995,96,2001,02 Free Software Foundation, Inc.

   Written by Miles Bader <miles@gnu.org>

   This program is free software; you can redistribute it and/or
   modify it under the terms of the GNU General Public License as
   published by the Free Software Foundation; either version 2, or (at
   your option) any later version.

   This program is distributed in the hope that it will be useful, but
   WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */

#include <hurd.h>
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>

#include "ps.h"
#include "common.h"

/* ---------------------------------------------------------------- */

/*
   The Basic & Sched info types are pretty must static so we cache them.
   However, as load info is dynamic, we do not cache it.

   See <mach/host_info.h> for information on the data types these routines
   return.
*/

/* Return the current host port.  */
mach_port_t
ps_get_host ()
{
  static mach_port_t host = MACH_PORT_NULL;
  if (host == MACH_PORT_NULL)
    host = mach_host_self ();
  return host;
}

/* Return a pointer to the basic info about the current host in INFO.
   Since this is static global information, we just use a static buffer.
   If a system error occurs, the error code is returned, otherwise 0 is
   returned.  */
error_t
ps_host_basic_info (host_basic_info_t *info)
{
  static int initialized;
  static host_basic_info_data_t buf;

  if (!initialized)
    {
      size_t size = sizeof (buf);
      error_t err = host_info (ps_get_host (), HOST_BASIC_INFO,
			      (host_info_t) &buf, &size);
      if (err)
	return err;
      initialized = 1;
    }

  *info = &buf;
  return 0;
}

/* Return a pointer to the scheduling info about the current host in INFO.
   Since this is static global information, we just use a static buffer.
   If a system error occurs, the error code is returned, otherwise 0 is
   returned.  */
error_t
ps_host_sched_info (host_sched_info_t *info)
{
  static int initialized;
  static host_sched_info_data_t buf;

  if (!initialized)
    {
      size_t size = sizeof (buf);
      error_t err = host_info (ps_get_host (), HOST_SCHED_INFO,
			      (host_info_t) &buf, &size);
      if (err)
	return err;
      initialized = 1;
    }

  *info = &buf;
  return 0;
}

/* Return a pointer to the load info about the current host in INFO.  Since
   this is global information, we just use a static buffer (if someone desires
   to keep old load info, they should copy the returned buffer).  If a system
   error occurs, the error code is returned, otherwise 0 is returned.  */
error_t
ps_host_load_info (host_load_info_t *info)
{
  static host_load_info_data_t buf;
  size_t size = sizeof (buf);
  error_t err = host_info (ps_get_host (), HOST_LOAD_INFO,
			  (host_info_t) &buf, &size);

  if (err)
    return err;

  *info = &buf;
  return 0;
}