4
* Copyright Ericsson AB 1996-2009. All Rights Reserved.
6
* The contents of this file are subject to the Erlang Public License,
7
* Version 1.1, (the "License"); you may not use this file except in
8
* compliance with the License. You should have received a copy of the
9
* Erlang Public License along with this software. If not, it can be
10
* retrieved online at http://www.erlang.org/.
12
* Software distributed under the License is distributed on an "AS IS"
13
* basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
14
* the License for the specific language governing rights and limitations
20
/* Purpose: Access to elib memory statistics */
27
#include "erl_driver.h"
28
#include "elib_stat.h"
30
#define MAP_BUF_SIZE 1000 /* Max map size */
31
#define HISTO_BUF_SIZE 100 /* Max histogram buckets */
33
static ErlDrvData mem_start(ErlDrvPort);
34
static int mem_init(void);
35
static void mem_stop(ErlDrvData);
36
static void mem_command(ErlDrvData, char*, int);
38
const struct driver_entry mem_driver_entry = {
48
static int mem_init(void)
53
static ErlDrvData mem_start(ErlDrvPort port, char* buf)
55
return (ErlDrvData)port;
58
static void mem_stop(ErlDrvData port)
65
p[0] = (v >> 24) & 0xff;
66
p[1] = (v >> 16) & 0xff;
67
p[2] = (v >> 8) & 0xff;
74
return (p[0] << 8) | p[1];
79
** m L1 L0 -> a heap map of length L1*256 + L0 is returned
80
** s -> X3 X2 X1 X0 Y3 Y2 Y1 Y0 Z3 Z2 Z1 Z0
81
** X == Total heap size bytes
82
** Y == Total free bytes
83
** Z == Size of largest free block in bytes
85
** h L1 L0 B0 -> Generate a logarithm historgram base B with L buckets
86
** l L1 L0 S0 -> Generate a linear histogram with step S with L buckets
88
unsigned char outbuf[HISTO_BUF_SIZE*2*4];
90
static void mem_command(ErlDrvData port, char* buf, int count)
92
if ((count == 1) && buf[0] == 's') {
93
struct elib_stat info;
98
putint32(v, info.mem_total*4);
99
putint32(v+4, info.mem_free*4);
100
putint32(v+8, info.max_free*4);
101
driver_output((ErlDrvPort)port, v, 12);
104
else if ((count == 3) && buf[0] == 'm') {
105
char w[MAP_BUF_SIZE];
106
int n = getint16(buf+1);
108
if (n > MAP_BUF_SIZE)
111
driver_output((ErlDrvPort)port, w, n);
114
else if ((count == 4) && (buf[0] == 'h' || buf[0] == 'l')) {
115
unsigned long vf[HISTO_BUF_SIZE];
116
unsigned long va[HISTO_BUF_SIZE];
117
int n = getint16(buf+1);
118
int base = (unsigned char) buf[3];
120
if (n >= HISTO_BUF_SIZE)
124
if (elib_histo(vf, va, n, base) < 0) {
125
driver_failure((ErlDrvPort)port, -1);
132
for (i = 0; i < n; i++) {
136
for (i = 0; i < n; i++) {
140
driver_output((ErlDrvPort)port, outbuf, n*8);
144
driver_failure((ErlDrvPort)port, -1);