Mercurial > hg > xemacs-beta
annotate src/getloadavg.c @ 5127:a9c41067dd88 ben-lisp-object
more cleanups, terminology clarification, lots of doc work
-------------------- ChangeLog entries follow: --------------------
man/ChangeLog addition:
2010-03-05 Ben Wing <ben@xemacs.org>
* internals/internals.texi (Introduction to Allocation):
* internals/internals.texi (Integers and Characters):
* internals/internals.texi (Allocation from Frob Blocks):
* internals/internals.texi (lrecords):
* internals/internals.texi (Low-level allocation):
Rewrite section on allocation of Lisp objects to reflect the new
reality. Remove references to nonexistent XSETINT and XSETCHAR.
modules/ChangeLog addition:
2010-03-05 Ben Wing <ben@xemacs.org>
* postgresql/postgresql.c (allocate_pgconn):
* postgresql/postgresql.c (allocate_pgresult):
* postgresql/postgresql.h (struct Lisp_PGconn):
* postgresql/postgresql.h (struct Lisp_PGresult):
* ldap/eldap.c (allocate_ldap):
* ldap/eldap.h (struct Lisp_LDAP):
Same changes as in src/ dir. See large log there in ChangeLog,
but basically:
ALLOC_LISP_OBJECT -> ALLOC_NORMAL_LISP_OBJECT
LISP_OBJECT_HEADER -> NORMAL_LISP_OBJECT_HEADER
../hlo/src/ChangeLog addition:
2010-03-05 Ben Wing <ben@xemacs.org>
* alloc.c:
* alloc.c (old_alloc_sized_lcrecord):
* alloc.c (very_old_free_lcrecord):
* alloc.c (copy_lisp_object):
* alloc.c (zero_sized_lisp_object):
* alloc.c (zero_nonsized_lisp_object):
* alloc.c (lisp_object_storage_size):
* alloc.c (free_normal_lisp_object):
* alloc.c (FREE_FIXED_TYPE_WHEN_NOT_IN_GC):
* alloc.c (ALLOC_FROB_BLOCK_LISP_OBJECT):
* alloc.c (Fcons):
* alloc.c (noseeum_cons):
* alloc.c (make_float):
* alloc.c (make_bignum):
* alloc.c (make_bignum_bg):
* alloc.c (make_ratio):
* alloc.c (make_ratio_bg):
* alloc.c (make_ratio_rt):
* alloc.c (make_bigfloat):
* alloc.c (make_bigfloat_bf):
* alloc.c (size_vector):
* alloc.c (make_compiled_function):
* alloc.c (Fmake_symbol):
* alloc.c (allocate_extent):
* alloc.c (allocate_event):
* alloc.c (make_key_data):
* alloc.c (make_button_data):
* alloc.c (make_motion_data):
* alloc.c (make_process_data):
* alloc.c (make_timeout_data):
* alloc.c (make_magic_data):
* alloc.c (make_magic_eval_data):
* alloc.c (make_eval_data):
* alloc.c (make_misc_user_data):
* alloc.c (Fmake_marker):
* alloc.c (noseeum_make_marker):
* alloc.c (size_string_direct_data):
* alloc.c (make_uninit_string):
* alloc.c (make_string_nocopy):
* alloc.c (mark_lcrecord_list):
* alloc.c (alloc_managed_lcrecord):
* alloc.c (free_managed_lcrecord):
* alloc.c (sweep_lcrecords_1):
* alloc.c (malloced_storage_size):
* buffer.c (allocate_buffer):
* buffer.c (compute_buffer_usage):
* buffer.c (DEFVAR_BUFFER_LOCAL_1):
* buffer.c (nuke_all_buffer_slots):
* buffer.c (common_init_complex_vars_of_buffer):
* buffer.h (struct buffer_text):
* buffer.h (struct buffer):
* bytecode.c:
* bytecode.c (make_compiled_function_args):
* bytecode.c (size_compiled_function_args):
* bytecode.h (struct compiled_function_args):
* casetab.c (allocate_case_table):
* casetab.h (struct Lisp_Case_Table):
* charset.h (struct Lisp_Charset):
* chartab.c (fill_char_table):
* chartab.c (Fmake_char_table):
* chartab.c (make_char_table_entry):
* chartab.c (copy_char_table_entry):
* chartab.c (Fcopy_char_table):
* chartab.c (put_char_table):
* chartab.h (struct Lisp_Char_Table_Entry):
* chartab.h (struct Lisp_Char_Table):
* console-gtk-impl.h (struct gtk_device):
* console-gtk-impl.h (struct gtk_frame):
* console-impl.h (struct console):
* console-msw-impl.h (struct Lisp_Devmode):
* console-msw-impl.h (struct mswindows_device):
* console-msw-impl.h (struct msprinter_device):
* console-msw-impl.h (struct mswindows_frame):
* console-msw-impl.h (struct mswindows_dialog_id):
* console-stream-impl.h (struct stream_console):
* console-stream.c (stream_init_console):
* console-tty-impl.h (struct tty_console):
* console-tty-impl.h (struct tty_device):
* console-tty.c (allocate_tty_console_struct):
* console-x-impl.h (struct x_device):
* console-x-impl.h (struct x_frame):
* console.c (allocate_console):
* console.c (nuke_all_console_slots):
* console.c (DEFVAR_CONSOLE_LOCAL_1):
* console.c (common_init_complex_vars_of_console):
* data.c (make_weak_list):
* data.c (make_weak_box):
* data.c (make_ephemeron):
* database.c:
* database.c (struct Lisp_Database):
* database.c (allocate_database):
* database.c (finalize_database):
* device-gtk.c (allocate_gtk_device_struct):
* device-impl.h (struct device):
* device-msw.c:
* device-msw.c (mswindows_init_device):
* device-msw.c (msprinter_init_device):
* device-msw.c (finalize_devmode):
* device-msw.c (allocate_devmode):
* device-tty.c (allocate_tty_device_struct):
* device-x.c (allocate_x_device_struct):
* device.c:
* device.c (nuke_all_device_slots):
* device.c (allocate_device):
* dialog-msw.c (handle_question_dialog_box):
* elhash.c:
* elhash.c (struct Lisp_Hash_Table):
* elhash.c (finalize_hash_table):
* elhash.c (make_general_lisp_hash_table):
* elhash.c (Fcopy_hash_table):
* elhash.h (htentry):
* emacs.c (main_1):
* eval.c:
* eval.c (size_multiple_value):
* event-stream.c (finalize_command_builder):
* event-stream.c (allocate_command_builder):
* event-stream.c (free_command_builder):
* event-stream.c (event_stream_generate_wakeup):
* event-stream.c (event_stream_resignal_wakeup):
* event-stream.c (event_stream_disable_wakeup):
* event-stream.c (event_stream_wakeup_pending_p):
* events.h (struct Lisp_Timeout):
* events.h (struct command_builder):
* extents-impl.h:
* extents-impl.h (struct extent_auxiliary):
* extents-impl.h (struct extent_info):
* extents-impl.h (set_extent_no_chase_aux_field):
* extents-impl.h (set_extent_no_chase_normal_field):
* extents.c:
* extents.c (gap_array_marker):
* extents.c (gap_array):
* extents.c (extent_list_marker):
* extents.c (extent_list):
* extents.c (stack_of_extents):
* extents.c (gap_array_make_marker):
* extents.c (extent_list_make_marker):
* extents.c (allocate_extent_list):
* extents.c (SLOT):
* extents.c (mark_extent_auxiliary):
* extents.c (allocate_extent_auxiliary):
* extents.c (attach_extent_auxiliary):
* extents.c (size_gap_array):
* extents.c (finalize_extent_info):
* extents.c (allocate_extent_info):
* extents.c (uninit_buffer_extents):
* extents.c (allocate_soe):
* extents.c (copy_extent):
* extents.c (vars_of_extents):
* extents.h:
* faces.c (allocate_face):
* faces.h (struct Lisp_Face):
* faces.h (struct face_cachel):
* file-coding.c:
* file-coding.c (finalize_coding_system):
* file-coding.c (sizeof_coding_system):
* file-coding.c (Fcopy_coding_system):
* file-coding.h (struct Lisp_Coding_System):
* file-coding.h (MARKED_SLOT):
* fns.c (size_bit_vector):
* font-mgr.c:
* font-mgr.c (finalize_fc_pattern):
* font-mgr.c (print_fc_pattern):
* font-mgr.c (Ffc_pattern_p):
* font-mgr.c (Ffc_pattern_create):
* font-mgr.c (Ffc_name_parse):
* font-mgr.c (Ffc_name_unparse):
* font-mgr.c (Ffc_pattern_duplicate):
* font-mgr.c (Ffc_pattern_add):
* font-mgr.c (Ffc_pattern_del):
* font-mgr.c (Ffc_pattern_get):
* font-mgr.c (fc_config_create_using):
* font-mgr.c (fc_strlist_to_lisp_using):
* font-mgr.c (fontset_to_list):
* font-mgr.c (Ffc_config_p):
* font-mgr.c (Ffc_config_up_to_date):
* font-mgr.c (Ffc_config_build_fonts):
* font-mgr.c (Ffc_config_get_cache):
* font-mgr.c (Ffc_config_get_fonts):
* font-mgr.c (Ffc_config_set_current):
* font-mgr.c (Ffc_config_get_blanks):
* font-mgr.c (Ffc_config_get_rescan_interval):
* font-mgr.c (Ffc_config_set_rescan_interval):
* font-mgr.c (Ffc_config_app_font_add_file):
* font-mgr.c (Ffc_config_app_font_add_dir):
* font-mgr.c (Ffc_config_app_font_clear):
* font-mgr.c (size):
* font-mgr.c (Ffc_config_substitute):
* font-mgr.c (Ffc_font_render_prepare):
* font-mgr.c (Ffc_font_match):
* font-mgr.c (Ffc_font_sort):
* font-mgr.c (finalize_fc_config):
* font-mgr.c (print_fc_config):
* font-mgr.h:
* font-mgr.h (struct fc_pattern):
* font-mgr.h (XFC_PATTERN):
* font-mgr.h (struct fc_config):
* font-mgr.h (XFC_CONFIG):
* frame-gtk.c (allocate_gtk_frame_struct):
* frame-impl.h (struct frame):
* frame-msw.c (mswindows_init_frame_1):
* frame-x.c (allocate_x_frame_struct):
* frame.c (nuke_all_frame_slots):
* frame.c (allocate_frame_core):
* gc.c:
* gc.c (GC_CHECK_NOT_FREE):
* glyphs.c (finalize_image_instance):
* glyphs.c (allocate_image_instance):
* glyphs.c (Fcolorize_image_instance):
* glyphs.c (allocate_glyph):
* glyphs.c (unmap_subwindow_instance_cache_mapper):
* glyphs.c (register_ignored_expose):
* glyphs.h (struct Lisp_Image_Instance):
* glyphs.h (struct Lisp_Glyph):
* glyphs.h (struct glyph_cachel):
* glyphs.h (struct expose_ignore):
* gui.c (allocate_gui_item):
* gui.h (struct Lisp_Gui_Item):
* keymap.c (struct Lisp_Keymap):
* keymap.c (make_keymap):
* lisp.h:
* lisp.h (struct Lisp_String_Direct_Data):
* lisp.h (struct Lisp_String_Indirect_Data):
* lisp.h (struct Lisp_Vector):
* lisp.h (struct Lisp_Bit_Vector):
* lisp.h (DECLARE_INLINE_LISP_BIT_VECTOR):
* lisp.h (struct weak_box):
* lisp.h (struct ephemeron):
* lisp.h (struct weak_list):
* lrecord.h:
* lrecord.h (struct lrecord_implementation):
* lrecord.h (MC_ALLOC_CALL_FINALIZER):
* lrecord.h (struct lcrecord_list):
* lstream.c (finalize_lstream):
* lstream.c (sizeof_lstream):
* lstream.c (Lstream_new):
* lstream.c (Lstream_delete):
* lstream.h (struct lstream):
* marker.c:
* marker.c (finalize_marker):
* marker.c (compute_buffer_marker_usage):
* mule-charset.c:
* mule-charset.c (make_charset):
* mule-charset.c (compute_charset_usage):
* objects-impl.h (struct Lisp_Color_Instance):
* objects-impl.h (struct Lisp_Font_Instance):
* objects-tty-impl.h (struct tty_color_instance_data):
* objects-tty-impl.h (struct tty_font_instance_data):
* objects-tty.c (tty_initialize_color_instance):
* objects-tty.c (tty_initialize_font_instance):
* objects.c (finalize_color_instance):
* objects.c (Fmake_color_instance):
* objects.c (finalize_font_instance):
* objects.c (Fmake_font_instance):
* objects.c (reinit_vars_of_objects):
* opaque.c:
* opaque.c (sizeof_opaque):
* opaque.c (make_opaque_ptr):
* opaque.c (free_opaque_ptr):
* opaque.h:
* opaque.h (Lisp_Opaque):
* opaque.h (Lisp_Opaque_Ptr):
* print.c (printing_unreadable_lcrecord):
* print.c (external_object_printer):
* print.c (debug_p4):
* process.c (finalize_process):
* process.c (make_process_internal):
* procimpl.h (struct Lisp_Process):
* rangetab.c (Fmake_range_table):
* rangetab.c (Fcopy_range_table):
* rangetab.h (struct Lisp_Range_Table):
* scrollbar.c:
* scrollbar.c (create_scrollbar_instance):
* scrollbar.c (compute_scrollbar_instance_usage):
* scrollbar.h (struct scrollbar_instance):
* specifier.c (finalize_specifier):
* specifier.c (sizeof_specifier):
* specifier.c (set_specifier_caching):
* specifier.h (struct Lisp_Specifier):
* specifier.h (struct specifier_caching):
* symeval.h:
* symeval.h (SYMBOL_VALUE_MAGIC_P):
* symeval.h (DEFVAR_SYMVAL_FWD):
* symsinit.h:
* syntax.c (init_buffer_syntax_cache):
* syntax.h (struct syntax_cache):
* toolbar.c:
* toolbar.c (allocate_toolbar_button):
* toolbar.c (update_toolbar_button):
* toolbar.h (struct toolbar_button):
* tooltalk.c (struct Lisp_Tooltalk_Message):
* tooltalk.c (make_tooltalk_message):
* tooltalk.c (struct Lisp_Tooltalk_Pattern):
* tooltalk.c (make_tooltalk_pattern):
* ui-gtk.c:
* ui-gtk.c (allocate_ffi_data):
* ui-gtk.c (emacs_gtk_object_finalizer):
* ui-gtk.c (allocate_emacs_gtk_object_data):
* ui-gtk.c (allocate_emacs_gtk_boxed_data):
* ui-gtk.h:
* window-impl.h (struct window):
* window-impl.h (struct window_mirror):
* window.c (finalize_window):
* window.c (allocate_window):
* window.c (new_window_mirror):
* window.c (mark_window_as_deleted):
* window.c (make_dummy_parent):
* window.c (compute_window_mirror_usage):
* window.c (compute_window_usage):
Overall point of this change and previous ones in this repository:
(1) Introduce new, clearer terminology: everything other than int
or char is a "record" object, which comes in two types: "normal
objects" and "frob-block objects". Fix up all places that
referred to frob-block objects as "simple", "basic", etc.
(2) Provide an advertised interface for doing operations on Lisp
objects, including creating new types, that is clean and
consistent in its naming, uses the above-referenced terms and
avoids referencing "lrecords", "old lcrecords", etc., which should
hide under the surface.
(3) Make the size_in_bytes and finalizer methods take a
Lisp_Object rather than a void * for consistency with other methods.
(4) Separate finalizer method into finalizer and disksaver, so
that normal finalize methods don't have to worry about disksaving.
Other specifics:
(1) Renaming:
LISP_OBJECT_HEADER -> NORMAL_LISP_OBJECT_HEADER
ALLOC_LISP_OBJECT -> ALLOC_NORMAL_LISP_OBJECT
implementation->basic_p -> implementation->frob_block_p
ALLOCATE_FIXED_TYPE_AND_SET_IMPL -> ALLOC_FROB_BLOCK_LISP_OBJECT
*FCCONFIG*, wrap_fcconfig -> *FC_CONFIG*, wrap_fc_config
*FCPATTERN*, wrap_fcpattern -> *FC_PATTERN*, wrap_fc_pattern
(the last two changes make the naming of these macros consistent
with the naming of all other macros, since the objects are named
fc-config and fc-pattern with a hyphen)
(2) Lots of documentation fixes in lrecord.h.
(3) Eliminate macros for copying, freeing, zeroing objects, getting
their storage size. Instead, new functions:
zero_sized_lisp_object()
zero_nonsized_lisp_object()
lisp_object_storage_size()
free_normal_lisp_object()
(copy_lisp_object() already exists)
LISP_OBJECT_FROB_BLOCK_P() (actually a macro)
Eliminated:
free_lrecord()
zero_lrecord()
copy_lrecord()
copy_sized_lrecord()
old_copy_lcrecord()
old_copy_sized_lcrecord()
old_zero_lcrecord()
old_zero_sized_lcrecord()
LISP_OBJECT_STORAGE_SIZE()
COPY_SIZED_LISP_OBJECT()
COPY_SIZED_LCRECORD()
COPY_LISP_OBJECT()
ZERO_LISP_OBJECT()
FREE_LISP_OBJECT()
(4) Catch the remaining places where lrecord stuff was used directly
and use the advertised interface, e.g. alloc_sized_lrecord() ->
ALLOC_SIZED_LISP_OBJECT().
(5) Make certain statically-declared pseudo-objects
(buffer_local_flags, console_local_flags) have their lheader
initialized correctly, so things like copy_lisp_object() can work
on them. Make extent_auxiliary_defaults a proper heap object
Vextent_auxiliary_defaults, and make extent auxiliaries dumpable
so that this object can be dumped. allocate_extent_auxiliary()
now just creates the object, and attach_extent_auxiliary()
creates an extent auxiliary and attaches to an extent, like the
old allocate_extent_auxiliary().
(6) Create EXTENT_AUXILIARY_SLOTS macro, similar to the foo-slots.h
files but in a macro instead of a file. The purpose is to avoid
duplication when iterating over all the slots in an extent auxiliary.
Use it.
(7) In lstream.c, don't zero out object after allocation because
allocation routines take care of this.
(8) In marker.c, fix a mistake in computing marker overhead.
(9) In print.c, clean up printing_unreadable_lcrecord(),
external_object_printer() to avoid lots of ifdef NEW_GC's.
(10) Separate toolbar-button allocation into a separate
allocate_toolbar_button() function for use in the example code
in lrecord.h.
author | Ben Wing <ben@xemacs.org> |
---|---|
date | Fri, 05 Mar 2010 04:08:17 -0600 |
parents | aa5ed11f473b |
children | 308d34e9f07d 861f2601a38b |
rev | line source |
---|---|
428 | 1 /* Get the system load averages. |
2 Copyright (C) 1985, 86, 87, 88, 89, 91, 92, 93, 1994, 1995 | |
3 Free Software Foundation, Inc. | |
4 | |
5 This file is part of XEmacs. | |
6 | |
7 XEmacs is free software; you can redistribute it and/or modify it | |
8 under the terms of the GNU General Public License as published by the | |
9 Free Software Foundation; either version 2, or (at your option) any | |
10 later version. | |
11 | |
12 XEmacs is distributed in the hope that it will be useful, but WITHOUT | |
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | |
15 for more details. | |
16 | |
17 You should have received a copy of the GNU General Public License | |
18 along with XEmacs; see the file COPYING. If not, write to | |
19 the Free Software Foundation, Inc., 59 Temple Place - Suite 330, | |
20 Boston, MA 02111-1307, USA. */ | |
21 | |
22 /* Compile-time symbols that this file uses: | |
23 | |
24 FIXUP_KERNEL_SYMBOL_ADDR() Adjust address in returned struct nlist. | |
25 KERNEL_FILE Pathname of the kernel to nlist. | |
26 LDAV_CVT() Scale the load average from the kernel. | |
27 Returns a double. | |
28 LDAV_SYMBOL Name of kernel symbol giving load average. | |
29 LOAD_AVE_TYPE Type of the load average array in the kernel. | |
4759
aa5ed11f473b
Remove support for obsolete systems. See xemacs-patches message with ID
Jerry James <james@xemacs.org>
parents:
1315
diff
changeset
|
30 Must be defined; otherwise, no load average |
aa5ed11f473b
Remove support for obsolete systems. See xemacs-patches message with ID
Jerry James <james@xemacs.org>
parents:
1315
diff
changeset
|
31 is available. |
428 | 32 NLIST_STRUCT Include nlist.h, not a.out.h, and |
33 the nlist n_name element is a pointer, | |
34 not an array. | |
35 LINUX_LDAV_FILE [__linux__]: File containing load averages. | |
36 | |
37 Specific system predefines this file uses, aside from setting | |
38 default values if not emacs: | |
39 | |
40 BSD Real BSD, not just BSD-like. | |
41 hpux | |
42 sgi | |
442 | 43 WIN32_NATIVE No-op for Windows9x/NT. |
44 CYGWIN No-op for Cygwin. | |
428 | 45 __linux__ Linux: assumes /proc filesystem mounted. |
46 Support from Michael K. Johnson. | |
47 __NetBSD__ NetBSD: assumes /kern filesystem mounted. | |
48 __OpenBSD__ OpenBSD: ditto. | |
49 | |
50 In addition, to avoid nesting many #ifdefs, we internally set | |
51 LDAV_DONE to indicate that the load average has been computed. | |
52 | |
53 We also #define LDAV_PRIVILEGED if a program will require | |
54 special installation to be able to call getloadavg. */ | |
55 | |
56 /* This should always be first. */ | |
57 #ifdef HAVE_CONFIG_H | |
58 #include <config.h> | |
59 #endif | |
60 | |
61 #include "lisp.h" | |
62 #include "sysfile.h" /* for encapsulated open, close, read, write */ | |
63 | |
64 #ifndef HAVE_GETLOADAVG | |
65 | |
66 /* The existing Emacs configuration files define a macro called | |
67 LOAD_AVE_CVT, which accepts a value of type LOAD_AVE_TYPE, and | |
68 returns the load average multiplied by 100. What we actually want | |
69 is a macro called LDAV_CVT, which returns the load average as an | |
70 unmultiplied double. | |
71 | |
72 For backwards compatibility, we'll define LDAV_CVT in terms of | |
73 LOAD_AVE_CVT, but future machine config files should just define | |
74 LDAV_CVT directly. */ | |
75 | |
76 #if !defined(LDAV_CVT) && defined(LOAD_AVE_CVT) | |
77 #define LDAV_CVT(n) (LOAD_AVE_CVT (n) / 100.0) | |
78 #endif | |
79 | |
80 #if defined (HAVE_KSTAT_H) | |
81 #include <kstat.h> | |
82 #endif /* HAVE_KSTAT_H */ | |
83 | |
84 /* Set values that are different from the defaults, which are | |
85 set a little farther down with #ifndef. */ | |
86 | |
87 | |
88 /* Some shorthands. */ | |
89 | |
90 #if defined (HPUX) && !defined (hpux) | |
91 #define hpux | |
92 #endif | |
93 | |
94 #if (defined(sun) && defined(SVR4)) || defined (SOLARIS2) | |
95 #define SUNOS_5 | |
96 #endif | |
97 | |
98 #if defined (__osf__) && (defined (__alpha) || defined (__alpha__)) | |
99 #define OSF_ALPHA | |
100 #include <netdb.h> | |
101 #include <netinet/in.h> /* Needed for Digital UNIX V3 */ | |
102 #include <net/proto_net.h> | |
103 #include <sys/table.h> | |
104 #endif | |
105 | |
106 #if defined (__osf__) && (defined (mips) || defined (__mips__)) | |
107 #define OSF_MIPS | |
108 #include <sys/table.h> | |
109 #endif | |
110 | |
111 | |
112 /* VAX C can't handle multi-line #ifs, or lines longer than 256 chars. */ | |
113 #ifndef LOAD_AVE_TYPE | |
114 | |
115 #ifdef sun | |
116 #define LOAD_AVE_TYPE long | |
117 #endif | |
118 | |
119 #ifdef decstation | |
120 #define LOAD_AVE_TYPE long | |
121 #endif | |
122 | |
123 #ifdef sgi | |
124 #define LOAD_AVE_TYPE long | |
125 #endif | |
126 | |
127 #ifdef SVR4 | |
128 #define LOAD_AVE_TYPE long | |
129 #endif | |
130 | |
131 #ifdef sony_news | |
132 #define LOAD_AVE_TYPE long | |
133 #endif | |
134 | |
135 #ifdef OSF_ALPHA | |
136 #define LOAD_AVE_TYPE long | |
137 #endif | |
138 | |
139 #if defined (ardent) && defined (titan) | |
140 #define LOAD_AVE_TYPE long | |
141 #endif | |
142 | |
143 #ifdef _AIX | |
144 #define LOAD_AVE_TYPE long | |
145 #endif | |
146 | |
147 #endif /* No LOAD_AVE_TYPE. */ | |
148 | |
149 #ifdef OSF_ALPHA | |
150 /* <sys/param.h> defines an incorrect value for FSCALE on Alpha OSF/1, | |
151 according to ghazi@noc.rutgers.edu. */ | |
152 #undef FSCALE | |
153 #define FSCALE 1024.0 | |
154 #endif | |
155 | |
156 #ifndef FSCALE | |
157 | |
158 /* SunOS and some others define FSCALE in sys/param.h. */ | |
159 | |
160 #if defined(MIPS) || defined(SVR4) || defined(decstation) | |
161 #define FSCALE 256 | |
162 #endif | |
163 | |
4759
aa5ed11f473b
Remove support for obsolete systems. See xemacs-patches message with ID
Jerry James <james@xemacs.org>
parents:
1315
diff
changeset
|
164 #if defined (sgi) |
428 | 165 /* Sometimes both MIPS and sgi are defined, so FSCALE was just defined |
166 above under #ifdef MIPS. But we want the sgi value. */ | |
167 #undef FSCALE | |
168 #define FSCALE 1000.0 | |
169 #endif | |
170 | |
171 #if defined (ardent) && defined (titan) | |
172 #define FSCALE 65536.0 | |
173 #endif | |
174 | |
175 #ifdef _AIX | |
176 #define FSCALE 65536.0 | |
177 #endif | |
178 | |
179 #endif /* Not FSCALE. */ | |
180 | |
181 #if !defined (LDAV_CVT) && defined (FSCALE) | |
182 #define LDAV_CVT(n) (((double) (n)) / FSCALE) | |
183 #endif | |
184 | |
185 /* VAX C can't handle multi-line #ifs, or lines longer that 256 characters. */ | |
186 #ifndef NLIST_STRUCT | |
187 | |
188 #ifdef sun | |
189 #define NLIST_STRUCT | |
190 #endif | |
191 | |
192 #ifdef decstation | |
193 #define NLIST_STRUCT | |
194 #endif | |
195 | |
196 #ifdef hpux | |
197 #define NLIST_STRUCT | |
198 #endif | |
199 | |
200 #ifdef sgi | |
201 #define NLIST_STRUCT | |
202 #endif | |
203 | |
204 #ifdef SVR4 | |
205 #define NLIST_STRUCT | |
206 #endif | |
207 | |
208 #ifdef sony_news | |
209 #define NLIST_STRUCT | |
210 #endif | |
211 | |
212 #ifdef OSF_ALPHA | |
213 #define NLIST_STRUCT | |
214 #endif | |
215 | |
216 #if defined (ardent) && defined (titan) | |
217 #define NLIST_STRUCT | |
218 #endif | |
219 | |
220 #ifdef butterfly | |
221 #define NLIST_STRUCT | |
222 #endif | |
223 | |
224 #ifdef _AIX | |
225 #define NLIST_STRUCT | |
226 #endif | |
227 | |
228 #endif /* defined (NLIST_STRUCT) */ | |
229 | |
230 | |
231 #if defined(sgi) || (defined(mips) && !defined(BSD)) | |
232 #define FIXUP_KERNEL_SYMBOL_ADDR(nl) ((nl)[0].n_value &= ~(1 << 31)) | |
233 #endif | |
234 | |
235 #if !defined (KERNEL_FILE) && defined (hpux) | |
236 #define KERNEL_FILE "/hp-ux" | |
237 #endif | |
238 | |
4759
aa5ed11f473b
Remove support for obsolete systems. See xemacs-patches message with ID
Jerry James <james@xemacs.org>
parents:
1315
diff
changeset
|
239 #if !defined(KERNEL_FILE) && (defined(MIPS) || defined(SVR4) || defined(ISC) || defined (sgi) || defined(SVR4) || (defined (ardent) && defined (titan))) |
428 | 240 #define KERNEL_FILE "/unix" |
241 #endif | |
242 | |
4759
aa5ed11f473b
Remove support for obsolete systems. See xemacs-patches message with ID
Jerry James <james@xemacs.org>
parents:
1315
diff
changeset
|
243 #if !defined(LDAV_SYMBOL) && (defined(hpux) || defined(SVR4) || defined(ISC) || defined(sgi) || (defined (ardent) && defined (titan)) || defined (_AIX)) |
428 | 244 #define LDAV_SYMBOL "avenrun" |
245 #endif | |
246 | |
247 #ifdef HAVE_UNISTD_H | |
248 #include <unistd.h> | |
249 #endif | |
250 | |
251 #include <stdio.h> | |
252 | |
253 /* LOAD_AVE_TYPE should only get defined if we're going to use the | |
254 nlist method. */ | |
255 #if !defined(LOAD_AVE_TYPE) && (defined(BSD) || defined(LDAV_CVT) || defined(KERNEL_FILE) || defined(LDAV_SYMBOL)) | |
256 #define LOAD_AVE_TYPE double | |
257 #endif | |
258 | |
259 #ifdef LOAD_AVE_TYPE | |
260 | |
261 #ifndef NLIST_STRUCT | |
262 #include <a.out.h> | |
263 #else /* NLIST_STRUCT */ | |
264 #include <nlist.h> | |
265 #endif /* NLIST_STRUCT */ | |
266 | |
267 #ifdef SUNOS_5 | |
268 #include <fcntl.h> | |
269 #include <kvm.h> | |
270 #endif | |
271 | |
272 #ifndef KERNEL_FILE | |
273 #define KERNEL_FILE "/vmunix" | |
274 #endif /* KERNEL_FILE */ | |
275 | |
276 #ifndef LDAV_SYMBOL | |
277 #define LDAV_SYMBOL "_avenrun" | |
278 #endif /* LDAV_SYMBOL */ | |
279 | |
280 #ifndef LDAV_CVT | |
281 #define LDAV_CVT(n) ((double) (n)) | |
282 #endif /* !LDAV_CVT */ | |
283 | |
284 #endif /* LOAD_AVE_TYPE */ | |
285 | |
286 #ifdef sgi | |
287 #include <sys/sysmp.h> | |
288 #endif /* sgi */ | |
289 | |
290 #if defined (HAVE_SYS_PSTAT_H) | |
291 #include <sys/pstat.h> | |
292 #endif /* HAVE_SYS_PSTAT_H (on HPUX) */ | |
293 | |
294 | |
295 /* Avoid static vars inside a function since in HPUX they dump as pure. */ | |
296 | |
297 #ifdef LOAD_AVE_TYPE | |
298 /* File descriptor open to /dev/kmem */ | |
299 static int channel; | |
300 /* Nonzero iff channel is valid. */ | |
301 static int getloadavg_initialized; | |
302 /* Offset in kmem to seek to read load average, or 0 means invalid. */ | |
303 static long offset; | |
304 | |
305 #ifndef sgi | |
306 static struct nlist nl[2]; | |
307 #endif /* not sgi */ | |
308 | |
309 #ifdef SUNOS_5 | |
310 static kvm_t *kd; | |
311 #endif /* SUNOS_5 */ | |
312 | |
313 #ifndef countof | |
314 # define countof(x) (sizeof (x) / sizeof (*(x))) | |
315 #endif | |
316 | |
317 #endif /* LOAD_AVE_TYPE */ | |
318 | |
319 /* Put the 1 minute, 5 minute and 15 minute load averages | |
320 into the first NELEM elements of LOADAVG. | |
321 Return the number written (never more than 3, but may be less than NELEM), | |
322 or -1 if an error occurred. */ | |
323 | |
442 | 324 int getloadavg (double loadavg[], int nelem); |
325 | |
428 | 326 int |
327 getloadavg (double loadavg[], int nelem) | |
328 { | |
329 int elem = 0; /* Return value. */ | |
330 | |
331 #ifdef NO_GET_LOAD_AVG | |
332 #define LDAV_DONE | |
333 /* Set errno to zero to indicate that there was no particular error; | |
334 this function just can't work at all on this system. */ | |
335 errno = 0; | |
336 elem = -2; | |
337 #endif /* NO_GET_LOAD_AVG */ | |
338 | |
339 #if ! defined (LDAV_DONE) && defined (HAVE_KSTAT_H) && defined (HAVE_LIBKSTAT) | |
340 #define LDAV_DONE | |
341 /* getloadavg is best implemented using kstat (kernel stats), on | |
342 systems (like SunOS5) that support it, since you don't need special | |
343 privileges to use it. | |
344 | |
345 Initial implementation courtesy Zlatko Calusic <zcalusic@carnet.hr>. | |
346 Integrated to XEmacs by Hrvoje Niksic <hniksic@xemacs.org>. | |
347 Additional cleanup by Hrvoje Niksic, based on code published by | |
348 Casper Dik <Casper.Dik@Holland.Sun.Com>. */ | |
349 kstat_ctl_t *kc; | |
350 kstat_t *ksp; | |
351 static char *avestrings[] = { "avenrun_1min", | |
352 "avenrun_5min", | |
353 "avenrun_15min" }; | |
354 | |
355 if (nelem > countof (avestrings)) | |
356 nelem = countof (avestrings); | |
357 | |
358 kc = kstat_open (); | |
359 if (!kc) | |
360 return -1; | |
361 ksp = kstat_lookup (kc, "unix", 0, "system_misc"); | |
362 if (!ksp) | |
363 { | |
364 kstat_close (kc); | |
365 return -1; | |
366 } | |
367 if (kstat_read (kc, ksp, 0) < 0) | |
368 { | |
369 kstat_close (kc); | |
370 return -1; | |
371 } | |
372 for (elem = 0; elem < nelem; elem++) | |
373 { | |
374 kstat_named_t *kn = | |
375 (kstat_named_t *) kstat_data_lookup (ksp, avestrings[elem]); | |
376 if (!kn) | |
377 { | |
378 kstat_close (kc); | |
379 return -1; | |
380 } | |
381 loadavg[elem] = (double)kn->value.ul / FSCALE; | |
382 } | |
383 kstat_close (kc); | |
384 #endif /* HAVE_KSTAT_H && HAVE_LIBKSTAT */ | |
385 | |
386 #if !defined (LDAV_DONE) && defined (HAVE_SYS_PSTAT_H) | |
387 #define LDAV_DONE | |
388 /* This is totally undocumented, and is not guaranteed to work, but | |
389 mayhap it might .... If it does work, it will work only on HP-UX | |
390 8.0 or later. -- Darryl Okahata <darrylo@sr.hp.com> */ | |
391 #undef LOAD_AVE_TYPE /* Make sure these don't exist. */ | |
392 #undef LOAD_AVE_CVT | |
393 #undef LDAV_SYMBOL | |
394 struct pst_dynamic procinfo; | |
395 union pstun statbuf; | |
396 | |
397 statbuf.pst_dynamic = &procinfo; | |
398 if (pstat (PSTAT_DYNAMIC, statbuf, sizeof (struct pst_dynamic), 0, 0) == -1) | |
399 return (-1); | |
400 loadavg[elem++] = procinfo.psd_avg_1_min; | |
401 loadavg[elem++] = procinfo.psd_avg_5_min; | |
402 loadavg[elem++] = procinfo.psd_avg_15_min; | |
403 #endif /* HPUX */ | |
404 | |
405 #if !defined (LDAV_DONE) && defined (__linux__) | |
406 #define LDAV_DONE | |
407 #undef LOAD_AVE_TYPE | |
408 | |
409 #ifndef LINUX_LDAV_FILE | |
410 #define LINUX_LDAV_FILE "/proc/loadavg" | |
411 #endif | |
412 | |
413 char ldavgbuf[40]; | |
414 double load_ave[3]; | |
415 int fd, count; | |
416 | |
771 | 417 fd = retry_open (LINUX_LDAV_FILE, O_RDONLY); |
428 | 418 if (fd == -1) |
419 return -1; | |
771 | 420 count = retry_read (fd, ldavgbuf, 40); |
421 (void) retry_close (fd); | |
428 | 422 if (count <= 0) |
423 return -1; | |
424 | |
425 count = sscanf (ldavgbuf, "%lf %lf %lf", | |
426 &load_ave[0], &load_ave[1], &load_ave[2]); | |
427 if (count < 1) | |
428 return -1; | |
429 | |
430 for (elem = 0; elem < nelem && elem < count; elem++) | |
431 loadavg[elem] = load_ave[elem]; | |
432 #endif /* __linux__ */ | |
433 | |
434 #if !defined (LDAV_DONE) && defined (__NetBSD__) || defined (__OpenBSD__) | |
435 #define LDAV_DONE | |
436 #undef LOAD_AVE_TYPE | |
437 | |
438 #ifndef NETBSD_LDAV_FILE | |
439 #define NETBSD_LDAV_FILE "/kern/loadavg" | |
440 #endif | |
441 | |
442 unsigned long int load_ave[3], scale; | |
443 int count; | |
444 FILE *fp; | |
445 | |
771 | 446 fp = retry_fopen (NETBSD_LDAV_FILE, "r"); |
428 | 447 if (fp == NULL) |
448 return -1; | |
449 count = fscanf (fp, "%lu %lu %lu %lu\n", | |
450 &load_ave[0], &load_ave[1], &load_ave[2], | |
451 &scale); | |
771 | 452 (void) retry_fclose (fp); |
428 | 453 if (count != 4) |
454 return -1; | |
455 | |
456 for (elem = 0; elem < nelem; elem++) | |
457 loadavg[elem] = (double) load_ave[elem] / (double) scale; | |
458 #endif /* __NetBSD__ or __OpenBSD__ */ | |
459 | |
1315 | 460 #if !defined (LDAV_DONE) && defined (WIN32_ANY) |
428 | 461 #define LDAV_DONE |
462 | |
463 /* A faithful emulation is going to have to be saved for a rainy day. */ | |
464 for ( ; elem < nelem; elem++) | |
465 { | |
466 loadavg[elem] = 0.0; | |
467 } | |
442 | 468 #endif /* WIN32_NATIVE or CYGWIN */ |
428 | 469 |
470 #if !defined (LDAV_DONE) && defined(LOAD_AVE_TYPE) | |
471 | |
472 /* UNIX-specific code -- read the average from /dev/kmem. */ | |
473 | |
474 #define LDAV_PRIVILEGED /* This code requires special installation. */ | |
475 | |
476 LOAD_AVE_TYPE load_ave[3]; | |
477 | |
478 /* Get the address of LDAV_SYMBOL. */ | |
479 if (offset == 0) | |
480 { | |
481 #ifndef sgi | |
482 #ifndef NLIST_STRUCT | |
483 strcpy (nl[0].n_name, LDAV_SYMBOL); | |
484 strcpy (nl[1].n_name, ""); | |
485 #else /* NLIST_STRUCT */ | |
486 nl[0].n_name = (char *) LDAV_SYMBOL; | |
487 nl[1].n_name = 0; | |
488 #endif /* NLIST_STRUCT */ | |
489 | |
490 #ifndef SUNOS_5 | |
491 if ( | |
492 #if !(defined (_AIX) && !defined (ps2)) | |
493 nlist (KERNEL_FILE, nl) | |
494 #else /* _AIX */ | |
495 knlist (nl, 1, sizeof (nl[0])) | |
496 #endif | |
497 >= 0) | |
498 /* Omit "&& nl[0].n_type != 0 " -- it breaks on Sun386i. */ | |
499 { | |
500 #ifdef FIXUP_KERNEL_SYMBOL_ADDR | |
501 FIXUP_KERNEL_SYMBOL_ADDR (nl); | |
502 #endif | |
503 offset = nl[0].n_value; | |
504 } | |
505 #endif /* !SUNOS_5 */ | |
506 #else /* sgi */ | |
507 int ldav_off; | |
508 | |
509 ldav_off = sysmp (MP_KERNADDR, MPKA_AVENRUN); | |
510 if (ldav_off != -1) | |
511 offset = (long) ldav_off & 0x7fffffff; | |
512 #endif /* sgi */ | |
513 } | |
514 | |
515 /* Make sure we have /dev/kmem open. */ | |
516 if (!getloadavg_initialized) | |
517 { | |
518 #ifndef SUNOS_5 | |
771 | 519 channel = retry_open ("/dev/kmem", 0); |
428 | 520 if (channel >= 0) |
521 { | |
522 /* Set the channel to close on exec, so it does not | |
523 litter any child's descriptor table. */ | |
524 #ifdef FD_SETFD | |
525 #ifndef FD_CLOEXEC | |
526 #define FD_CLOEXEC 1 | |
527 #endif | |
528 (void) fcntl (channel, F_SETFD, FD_CLOEXEC); | |
529 #endif | |
530 getloadavg_initialized = 1; | |
531 } | |
532 #else /* SUNOS_5 */ | |
533 /* We pass 0 for the kernel, corefile, and swapfile names | |
534 to use the currently running kernel. */ | |
535 kd = kvm_open (0, 0, 0, O_RDONLY, 0); | |
536 if (kd != 0) | |
537 { | |
538 /* nlist the currently running kernel. */ | |
539 kvm_nlist (kd, nl); | |
540 offset = nl[0].n_value; | |
541 getloadavg_initialized = 1; | |
542 } | |
543 #endif /* SUNOS_5 */ | |
544 } | |
545 | |
546 /* If we can, get the load average values. */ | |
547 if (offset && getloadavg_initialized) | |
548 { | |
549 /* Try to read the load. */ | |
550 #ifndef SUNOS_5 | |
551 if (lseek (channel, offset, 0) == -1L | |
771 | 552 || retry_read (channel, (char *) load_ave, sizeof (load_ave)) |
428 | 553 != sizeof (load_ave)) |
554 { | |
771 | 555 retry_close (channel); |
428 | 556 getloadavg_initialized = 0; |
557 } | |
558 #else /* SUNOS_5 */ | |
559 if (kvm_read (kd, offset, (char *) load_ave, sizeof (load_ave)) | |
560 != sizeof (load_ave)) | |
561 { | |
562 kvm_close (kd); | |
563 getloadavg_initialized = 0; | |
564 } | |
565 #endif /* SUNOS_5 */ | |
566 } | |
567 | |
568 if (offset == 0 || !getloadavg_initialized) | |
569 return -1; | |
570 | |
571 if (nelem > 0) | |
572 loadavg[elem++] = LDAV_CVT (load_ave[0]); | |
573 if (nelem > 1) | |
574 loadavg[elem++] = LDAV_CVT (load_ave[1]); | |
575 if (nelem > 2) | |
576 loadavg[elem++] = LDAV_CVT (load_ave[2]); | |
577 | |
578 #define LDAV_DONE | |
579 #endif /* !LDAV_DONE && LOAD_AVE_TYPE */ | |
580 | |
581 return elem; | |
582 } | |
583 | |
584 #endif /* ! HAVE_GETLOADAVG */ | |
585 | |
586 #ifdef TEST | |
587 void | |
588 main (int argc, char **argv) | |
589 { | |
590 int naptime = 0; | |
591 | |
592 if (argc > 1) | |
593 naptime = atoi (argv[1]); | |
594 | |
595 while (1) | |
596 { | |
597 double avg[3]; | |
598 int loads; | |
599 | |
600 errno = 0; /* Don't be misled if it doesn't set errno. */ | |
601 loads = getloadavg (avg, 3); | |
602 if (loads == -1) | |
603 { | |
604 perror ("Error getting load average"); | |
605 exit (1); | |
606 } | |
607 if (loads > 0) | |
608 printf ("1-minute: %f ", avg[0]); | |
609 if (loads > 1) | |
610 printf ("5-minute: %f ", avg[1]); | |
611 if (loads > 2) | |
612 printf ("15-minute: %f ", avg[2]); | |
613 if (loads > 0) | |
614 putchar ('\n'); | |
615 | |
616 if (naptime == 0) | |
617 break; | |
618 sleep (naptime); | |
619 } | |
620 | |
621 exit (0); | |
622 } | |
623 #endif /* TEST */ |