2 * Copyright © 2008 Intel Corporation
3 * Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved.
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
12 * The above copyright notice and this permission notice (including the next
13 * paragraph) shall be included in all copies or substantial portions of the
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22 * DEALINGS IN THE SOFTWARE.
25 * Eric Anholt <eric@anholt.net>
40 #include <sys/ioctl.h>
44 #ifdef HAVE_STRUCT_SYSINFO_TOTALRAM
45 #include <sys/sysinfo.h>
46 #elif defined(HAVE_SWAPCTL) /* Solaris */
55 * intel_get_total_ram_mb:
58 * The total amount of system RAM available in MB.
61 intel_get_total_ram_mb(void)
65 #ifdef HAVE_STRUCT_SYSINFO_TOTALRAM /* Linux */
66 struct sysinfo sysinf;
69 ret = sysinfo(&sysinf);
72 retval = sysinf.totalram;
73 retval *= sysinf.mem_unit;
74 #elif defined(_SC_PAGESIZE) && defined(_SC_PHYS_PAGES) /* Solaris */
75 long pagesize, npages;
77 pagesize = sysconf(_SC_PAGESIZE);
78 npages = sysconf(_SC_PHYS_PAGES);
80 retval = (uint64_t) pagesize * npages;
82 #error "Unknown how to get RAM size for this OS"
85 return retval / (1024*1024);
89 * intel_get_avail_ram_mb:
92 * The amount of unused system RAM available in MB.
95 intel_get_avail_ram_mb(void)
99 #ifdef HAVE_STRUCT_SYSINFO_TOTALRAM /* Linux */
100 struct sysinfo sysinf;
103 ret = sysinfo(&sysinf);
106 retval = sysinf.freeram;
107 retval *= sysinf.mem_unit;
108 #elif defined(_SC_PAGESIZE) && defined(_SC_PHYS_PAGES) /* Solaris */
109 long pagesize, npages;
111 pagesize = sysconf(_SC_PAGESIZE);
112 npages = sysconf(_SC_AVPHYS_PAGES);
114 retval = (uint64_t) pagesize * npages;
116 #error "Unknown how to get available RAM for this OS"
119 return retval / (1024*1024);
123 * intel_get_total_swap_mb:
126 * The total amount of swap space available in MB.
129 intel_get_total_swap_mb(void)
133 #ifdef HAVE_STRUCT_SYSINFO_TOTALRAM /* Linux */
134 struct sysinfo sysinf;
137 ret = sysinfo(&sysinf);
140 retval = sysinf.freeswap;
141 retval *= sysinf.mem_unit;
142 #elif defined(HAVE_SWAPCTL) /* Solaris */
143 long pagesize = sysconf(_SC_PAGESIZE);
144 uint64_t totalpages = 0;
149 if ((n = swapctl(SC_GETNSWP, NULL)) == -1) {
150 perror("swapctl: GETNSWP");
154 /* no error, but no swap devices either */
158 swt = malloc(sizeof(struct swaptable) + (n * sizeof(swapent_t)));
159 buf = malloc(n * MAXPATHLEN);
164 for (i = 0 ; i < n; i++) {
165 swt->swt_ent[i].ste_path = buf + (i * MAXPATHLEN);
168 if ((n = swapctl(SC_LIST, swt)) == -1) {
169 perror("swapctl: LIST");
171 for (i = 0; i < swt->swt_n; i++) {
172 totalpages += swt->swt_ent[i].ste_pages;
179 retval = (uint64_t) pagesize * totalpages;
181 #warning "Unknown how to get swap size for this OS"
185 return retval / (1024*1024);
189 intel_purge_vm_caches(void)
193 fd = open("/proc/sys/vm/drop_caches", O_RDWR);
203 * When testing a port to a new platform, create a standalone test binary
205 * cc -o porttest intel_drm.c -I.. -DSTANDALONE_TEST `pkg-config --cflags libdrm`
206 * and then running the resulting porttest program.
208 #ifdef STANDALONE_TEST
211 int main(int argc, char **argv)
213 printf("Total RAM: %" PRIu64 " Mb\n", intel_get_total_ram_mb());
214 printf("Total Swap: %" PRIu64 " Mb\n", intel_get_total_swap_mb());
218 #endif /* STANDALONE_TEST */