xapian-core  1.4.27
freemem.cc
Go to the documentation of this file.
1 
4 /* Copyright (C) 2007,2008,2009,2010,2020 Olly Betts
5  * Copyright (C) 2008 Lemur Consulting Ltd
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20  */
21 
22 #include <config.h>
23 
24 #include "freemem.h"
25 
26 #include <sys/types.h>
27 #include "safeunistd.h"
28 #ifdef HAVE_SYS_SYSCTL_H
29 // Linux also has sys/sysctl.h but newer versions give a deprecation warning.
30 # ifndef __linux__
31 # include <sys/sysctl.h>
32 # endif
33 #endif
34 #ifdef HAVE_VM_VM_PARAM_H
35 # include <vm/vm_param.h>
36 #endif
37 #ifdef HAVE_SYS_VMMETER_H
38 # include <sys/vmmeter.h>
39 #endif
40 #ifdef HAVE_SYS_SYSMP_H
41 # include <sys/sysmp.h>
42 #endif
43 #ifdef HAVE_SYS_SYSINFO_H
44 # include <sys/sysinfo.h>
45 #endif
46 #ifdef HAVE_SYS_PSTAT_H
47 # include <sys/pstat.h>
48 #endif
49 
50 #ifdef __WIN32__
51 # include "safewindows.h"
52 #endif
53 
54 /* Tested on:
55  * Linux, FreeBSD, HP-UX, Microsoft Windows.
56  */
57 
58 long long
60 {
61 #ifndef __WIN32__
62  long long pagesize = 1;
63  long long pages = -1;
64 #if defined(_SC_PAGESIZE) && defined(_SC_PHYS_PAGES)
65  /* Linux:
66  * _SC_AVPHYS_PAGES is "available memory", but that excludes memory being
67  * used by the OS VM cache, which will often be almost all memory which
68  * isn't otherwise used, so used _SC_PHYS_PAGES which is just memory -
69  * that's good enough for Omega's use where we really just want to avoid
70  * runaway filter processes for dragging down the system.
71  */
72  pagesize = sysconf(_SC_PAGESIZE);
73  pages = sysconf(_SC_PHYS_PAGES);
74 #elif defined HAVE_PSTAT_GETDYNAMIC
75  /* HP-UX: */
76  struct pst_dynamic info;
77  if (pstat_getdynamic(&info, sizeof(info), 1, 0) == 1) {
78  pagesize = getpagesize();
79  pages = info.psd_free;
80  }
81 #elif defined CTL_VM && (defined VM_TOTAL || defined VM_METER)
82  /* FreeBSD: */
83  struct vmtotal vm_info;
84  static const int mib[2] = {
85  CTL_VM,
86 #ifdef VM_TOTAL
87  VM_TOTAL
88 #else
89  VM_METER
90 #endif
91  };
92  size_t len = sizeof(vm_info);
93  if (sysctl(mib, 2, &vm_info, &len, NULL, 0) == 0) {
94  pagesize = getpagesize();
95  pages = vm_info.t_free;
96  }
97 #endif
98  if (pagesize > 0 && pages > 0) {
99  return pages * pagesize;
100  }
101  return -1;
102 #else
103  MEMORYSTATUSEX statex;
104  statex.dwLength = sizeof(statex);
105  GlobalMemoryStatusEx(&statex);
106  return statex.ullAvailPhys;
107 #endif
108 }
109 
110 /* Tested on:
111  * Linux, Microsoft Windows.
112  */
113 
114 long long
116 {
117 #ifndef __WIN32__
118  long long pagesize = 1;
119  long long pages = -1;
120 #if defined(_SC_PAGESIZE) && defined(_SC_AVPHYS_PAGES)
121  /* Linux: */
122  pagesize = sysconf(_SC_PAGESIZE);
123  pages = sysconf(_SC_PHYS_PAGES);
124 #elif defined HAVE_PSTAT_GETDYNAMIC
125  /* HP-UX: */
126  struct pst_dynamic info;
127  if (pstat_getdynamic(&info, sizeof(info), 1, 0) == 1) {
128  pagesize = getpagesize();
129  pages = info.psd_rm;
130  }
131 #elif defined CTL_VM && (defined VM_TOTAL || defined VM_METER)
132  /* FreeBSD: */
133  struct vmtotal vm_info;
134  static const int mib[2] = {
135  CTL_VM,
136 #ifdef VM_TOTAL
137  VM_TOTAL
138 #else
139  VM_METER
140 #endif
141  };
142  size_t len = sizeof(vm_info);
143  if (sysctl(mib, 2, &vm_info, &len, NULL, 0) == 0) {
144  pagesize = getpagesize();
145  pages = vm_info.t_rm;
146  }
147 #endif
148  if (pagesize > 0 && pages > 0) {
149  return pages * pagesize;
150  }
151  return -1;
152 #else
153  MEMORYSTATUSEX statex;
154  statex.dwLength = sizeof(statex);
155  GlobalMemoryStatusEx(&statex);
156  return statex.ullTotalPhys;
157 #endif
158 }
long long get_total_physical_memory()
Determine how much physical memory there is.
Definition: freemem.cc:115
include <windows.h> without all the bloat and damage.
<unistd.h>, but with compat.
determine how much free physical memory there is.
long long get_free_physical_memory()
Determine how much free physical memory there is.
Definition: freemem.cc:59