Changed client <-> server interface to make less system calls.
1998-06-18 Martin Baulig <baulig@taurus.uni-trier.de> * lib/{command, write, read}.c: Changed client <-> server interface to make less system calls. * src/daemon/main.c: Changed server side of interface. * include/glibtop/command.h (struct _glibtop_response): New structure to return data from the server to the client.
This commit is contained in:
committed by
Martin Baulig
parent
d9e6288b7a
commit
2de9ea5c7d
10
ChangeLog
10
ChangeLog
@@ -1,3 +1,13 @@
|
||||
1998-06-18 Martin Baulig <baulig@taurus.uni-trier.de>
|
||||
|
||||
* lib/{command, write, read}.c: Changed client <-> server
|
||||
interface to make less system calls.
|
||||
|
||||
* src/daemon/main.c: Changed server side of interface.
|
||||
|
||||
* include/glibtop/command.h (struct _glibtop_response): New
|
||||
structure to return data from the server to the client.
|
||||
|
||||
1998-06-14 Martin Baulig <martin@home-of-linux.org>
|
||||
|
||||
* glibtop.h (struct _glibtop): Added `socket' field.
|
||||
|
@@ -50,13 +50,29 @@ __BEGIN_DECLS
|
||||
|
||||
#define GLIBTOP_MAX_CMND 18
|
||||
|
||||
#define _GLIBTOP_PARAM_SIZE 16
|
||||
|
||||
typedef struct _glibtop_command glibtop_command;
|
||||
typedef struct _glibtop_response glibtop_response;
|
||||
|
||||
struct _glibtop_command
|
||||
{
|
||||
glibtop server;
|
||||
unsigned command;
|
||||
size_t size;
|
||||
size_t size, data_size;
|
||||
char parameter [_GLIBTOP_PARAM_SIZE];
|
||||
};
|
||||
|
||||
union _glibtop_response_union
|
||||
{
|
||||
glibtop_union data;
|
||||
glibtop_sysdeps sysdeps;
|
||||
};
|
||||
|
||||
struct _glibtop_response
|
||||
{
|
||||
size_t data_size;
|
||||
union _glibtop_response_union u;
|
||||
};
|
||||
|
||||
#define glibtop_call(p1, p2, p3, p4) glibtop_call_r(glibtop_global_server, p1, p2, p3, p4)
|
||||
|
@@ -4,7 +4,7 @@ INCLUDES = -I$(top_builddir) -I$(top_srcdir) @machine_incs@ \
|
||||
-I$(top_srcdir)/include -I$(top_srcdir)/intl @GUILE_INCS@ \
|
||||
-DGTOPLOCALEDIR=\"$(datadir)/locale\" -D_GNU_SOURCE
|
||||
|
||||
CFLAGS = -Wall -W @CFLAGS@ -DGTOP_SERVER=\""@LIBGTOP_SERVER@"\"
|
||||
CFLAGS = -Wall -W @CFLAGS@ -DGTOP_SERVER=\""@LIBGTOP_SERVER@"\" -DDEBUG
|
||||
|
||||
lib_LTLIBRARIES = libgtop.la
|
||||
|
||||
|
@@ -30,25 +30,41 @@ void *
|
||||
glibtop_call_l (glibtop *server, unsigned command, size_t send_size, void *send_buf,
|
||||
size_t recv_size, void *recv_buf)
|
||||
{
|
||||
glibtop_command *cmnd;
|
||||
glibtop_command cmnd;
|
||||
glibtop_response response;
|
||||
void *ptr;
|
||||
|
||||
glibtop_init_r (&server, 0, 0);
|
||||
|
||||
cmnd = glibtop_calloc_r (server, 1, sizeof (glibtop_command));
|
||||
memset (&cmnd, 0, sizeof (glibtop_command));
|
||||
|
||||
memcpy (&cmnd->server, server, sizeof (glibtop));
|
||||
memcpy (&cmnd.server, server, sizeof (glibtop));
|
||||
|
||||
cmnd->command = command;
|
||||
cmnd->size = send_size;
|
||||
cmnd.command = command;
|
||||
|
||||
glibtop_write_l (server, sizeof (glibtop_command), cmnd);
|
||||
/* If send_size is less than _GLIBTOP_PARAM_SIZE (normally 16 Bytes), we
|
||||
* send it together with command, so we only need one system call instead
|
||||
* of two. */
|
||||
|
||||
#ifdef DEBUG
|
||||
fprintf (stderr, "COMMAND: send_size = %d; command = %d; sizeof (cmnd) = %d\n",
|
||||
send_size, command, sizeof (glibtop_command));
|
||||
#endif
|
||||
|
||||
|
||||
if (send_size <= _GLIBTOP_PARAM_SIZE)
|
||||
memcpy (cmnd.parameter, send_buf, send_size);
|
||||
else
|
||||
cmnd.size = send_size;
|
||||
|
||||
glibtop_write_l (server, sizeof (glibtop_command), &cmnd);
|
||||
glibtop_write_l (server, send_size, send_buf);
|
||||
glibtop_read_l (server, recv_size, recv_buf);
|
||||
|
||||
ptr = glibtop_read_data_l (server);
|
||||
glibtop_read_l (server, sizeof (glibtop_response), &response);
|
||||
|
||||
glibtop_free_r (server, cmnd);
|
||||
/* glibtop_read_l (server, recv_size, recv_buf); */
|
||||
|
||||
return ptr;
|
||||
/* ptr = glibtop_read_data_l (server); */
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
17
lib/read.c
17
lib/read.c
@@ -26,27 +26,10 @@
|
||||
void
|
||||
glibtop_read_l (glibtop *server, size_t size, void *buf)
|
||||
{
|
||||
size_t ssize;
|
||||
int ret;
|
||||
|
||||
glibtop_init_r (&server, 0, 0);
|
||||
|
||||
#ifdef DEBUG
|
||||
fprintf (stderr, "LIBRARY: reading %d bytes, should be %d.\n", sizeof (size_t), size);
|
||||
#endif
|
||||
|
||||
if (server->socket) {
|
||||
ret = recv (server->socket, &ssize, sizeof (size_t), 0);
|
||||
} else {
|
||||
ret = read (server->input [0], &ssize, sizeof (size_t));
|
||||
}
|
||||
|
||||
if (ret < 0)
|
||||
glibtop_error_io_r (server, _("read size"));
|
||||
|
||||
if (size != ssize)
|
||||
glibtop_error_r (server, _("got %d bytes but requested %d (ret = %d)"), ssize, size, ret);
|
||||
|
||||
#ifdef DEBUG
|
||||
fprintf (stderr, "LIBRARY: really reading %d bytes.\n", size);
|
||||
#endif
|
||||
|
15
lib/write.c
15
lib/write.c
@@ -30,20 +30,7 @@ glibtop_write_l (glibtop *server, size_t size, void *buf)
|
||||
|
||||
glibtop_init_r (&server, 0, 0);
|
||||
|
||||
#ifdef DEBUG
|
||||
fprintf (stderr, "LIBRARY: writing %d bytes = %d.\n", sizeof (size_t), size);
|
||||
#endif
|
||||
|
||||
if (server->socket) {
|
||||
ret = send (server->socket, &size, sizeof (size_t), 0);
|
||||
} else {
|
||||
ret = write (server->output [1], &size, sizeof (size_t));
|
||||
}
|
||||
|
||||
if (ret < 0)
|
||||
glibtop_error_io_r (server, _("write size"));
|
||||
|
||||
if (!size) return;
|
||||
if (size == 0) return;
|
||||
|
||||
#ifdef DEBUG
|
||||
fprintf (stderr, "LIBRARY: really writing %d bytes.\n", size);
|
||||
|
@@ -4,7 +4,7 @@ INCLUDES = -I$(top_builddir) -I$(top_srcdir) @machine_incs@ \
|
||||
-I$(top_srcdir)/include -I$(top_srcdir)/intl @GUILE_INCS@ \
|
||||
-DGTOPLOCALEDIR=\"$(datadir)/locale\" -D_GNU_SOURCE
|
||||
|
||||
CFLAGS = -Wall -W @CFLAGS@ -DGTOP_SERVER=\""@LIBGTOP_SERVER@"\"
|
||||
CFLAGS = -Wall -W @CFLAGS@ -DGTOP_SERVER=\""@LIBGTOP_SERVER@"\" -DDEBUG -DREAL_DEBUG
|
||||
|
||||
bin_PROGRAMS = gnuserv
|
||||
|
||||
|
@@ -39,37 +39,37 @@
|
||||
#endif
|
||||
|
||||
static void
|
||||
do_output (int s, size_t size, const void *buf)
|
||||
do_output (int s, glibtop_response *response, size_t data_size, const void *data)
|
||||
{
|
||||
#ifdef REAL_DEBUG
|
||||
fprintf (stderr, "Writing %d bytes = %d.\n", sizeof (size_t), size);
|
||||
fprintf (stderr, "Really writing %d bytes.\n", sizeof (glibtop_response));
|
||||
#endif
|
||||
|
||||
if (send (s, &size, sizeof (size_t), 0) < 0)
|
||||
response->data_size = data_size;
|
||||
|
||||
if (send (s, response, sizeof (glibtop_response), 0) < 0)
|
||||
glibtop_warn_io ("send");
|
||||
|
||||
if (!size)
|
||||
return;
|
||||
|
||||
if (response->data_size) {
|
||||
#ifdef REAL_DEBUG
|
||||
fprintf (stderr, "Really writing %d bytes.\n", size);
|
||||
fprintf (stderr, "Writing %d bytes of data.\n", response->data_size);
|
||||
#endif
|
||||
|
||||
if (send (s, buf, size, 0) < 0)
|
||||
if (send (s, data, response->data_size, 0) , 0)
|
||||
glibtop_warn_io ("send");
|
||||
}
|
||||
}
|
||||
|
||||
void
|
||||
handle_socket_connection (int s)
|
||||
{
|
||||
pid_t pid;
|
||||
int nread;
|
||||
size_t size, dsize;
|
||||
size_t size;
|
||||
char parameter [BUFSIZ];
|
||||
struct timeval tv;
|
||||
glibtop_union data;
|
||||
glibtop_response response;
|
||||
glibtop_command cmnd;
|
||||
glibtop_sysdeps sysdeps;
|
||||
glibtop server;
|
||||
void *ptr;
|
||||
|
||||
@@ -77,7 +77,7 @@ handle_socket_connection (int s)
|
||||
tv.tv_usec = 0;
|
||||
|
||||
while(1) {
|
||||
nread = recv (s, &size, sizeof (size_t), 0);
|
||||
nread = recv (s, &cmnd, sizeof (glibtop_command), 0);
|
||||
|
||||
if (nread < 0) {
|
||||
glibtop_warn_io ("recv");
|
||||
@@ -87,55 +87,27 @@ handle_socket_connection (int s)
|
||||
if (nread == 0)
|
||||
return;
|
||||
|
||||
if (nread != sizeof (size_t)) {
|
||||
glibtop_warn ("Expected %d bytes but got %d", sizeof (size_t), nread);
|
||||
return;
|
||||
}
|
||||
|
||||
if (size != sizeof (glibtop_command)) {
|
||||
glibtop_warn ("Expected %d bytes but got %d", sizeof (size_t), nread);
|
||||
return;
|
||||
}
|
||||
|
||||
nread = recv (s, &cmnd, size, 0);
|
||||
|
||||
if (nread < 0) {
|
||||
glibtop_warn_io ("recv");
|
||||
return;
|
||||
}
|
||||
|
||||
if (nread == 0)
|
||||
return;
|
||||
if (nread != sizeof (glibtop_command))
|
||||
glibtop_warn ("Received %d bytes, but expected %d\n",
|
||||
nread, sizeof (glibtop_command));
|
||||
|
||||
#ifdef REAL_DEBUG
|
||||
fprintf (stderr, "Received command %d from client.\n", cmnd.command);
|
||||
#endif
|
||||
|
||||
nread = recv (s, &dsize, sizeof (size_t), 0);
|
||||
|
||||
/* will return 0 if parent exits. */
|
||||
|
||||
if (nread < 0) {
|
||||
glibtop_warn_io ("recv");
|
||||
return;
|
||||
}
|
||||
|
||||
if (nread == 0)
|
||||
return;
|
||||
|
||||
if (dsize >= BUFSIZ) {
|
||||
glibtop_warn ("Client sent %d bytes, but buffer is %d", dsize, BUFSIZ);
|
||||
if (cmnd.size >= BUFSIZ) {
|
||||
glibtop_warn ("Client sent %d bytes, but buffer is %d", cmnd.size, BUFSIZ);
|
||||
return;
|
||||
}
|
||||
|
||||
memset (parameter, 0, sizeof (parameter));
|
||||
|
||||
if (dsize) {
|
||||
if (cmnd.size) {
|
||||
#ifdef REAL_DEBUG
|
||||
fprintf (stderr, "Client has %d bytes of data.\n", dsize);
|
||||
fprintf (stderr, "Client has %d bytes of data.\n", cmnd.size);
|
||||
#endif
|
||||
|
||||
nread = recv (s, parameter, dsize, 0);
|
||||
nread = recv (s, parameter, cmnd.size, 0);
|
||||
|
||||
/* will return 0 if parent exits. */
|
||||
|
||||
@@ -147,112 +119,96 @@ handle_socket_connection (int s)
|
||||
if (nread == 0)
|
||||
return;
|
||||
|
||||
if (nread != (int) dsize) {
|
||||
glibtop_warn ("Expected %d bytes but got %d", dsize, nread);
|
||||
if (nread != (int) cmnd.size) {
|
||||
glibtop_warn ("Expected %d bytes but got %d",
|
||||
cmnd.size, nread);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
switch (cmnd.command) {
|
||||
case GLIBTOP_CMND_SYSDEPS:
|
||||
sysdeps.features = GLIBTOP_SYSDEPS_ALL;
|
||||
do_output (s, sizeof (glibtop_sysdeps), &sysdeps);
|
||||
do_output (s, 0, NULL);
|
||||
response.u.sysdeps.features = GLIBTOP_SYSDEPS_ALL;
|
||||
do_output (s, &response, 0, NULL);
|
||||
break;
|
||||
case GLIBTOP_CMND_CPU:
|
||||
glibtop_get_cpu_l (&server, &data.cpu);
|
||||
do_output (s, sizeof (glibtop_cpu), &data.cpu);
|
||||
do_output (s, 0, NULL);
|
||||
glibtop_get_cpu_l (&server, &response.u.data.cpu);
|
||||
do_output (s, &response, 0, NULL);
|
||||
break;
|
||||
case GLIBTOP_CMND_MEM:
|
||||
glibtop_get_mem_l (&server, &data.mem);
|
||||
do_output (s, sizeof (glibtop_mem), &data.mem);
|
||||
do_output (s, 0, NULL);
|
||||
glibtop_get_mem_l (&server, &response.u.data.mem);
|
||||
do_output (s, &response, 0, NULL);
|
||||
break;
|
||||
case GLIBTOP_CMND_SWAP:
|
||||
glibtop_get_swap_l (&server, &data.swap);
|
||||
do_output (s, sizeof (glibtop_swap), &data.swap);
|
||||
do_output (s, 0, NULL);
|
||||
glibtop_get_swap_l (&server, &response.u.data.swap);
|
||||
do_output (s, &response, 0, NULL);
|
||||
break;
|
||||
case GLIBTOP_CMND_UPTIME:
|
||||
glibtop_get_uptime_l (&server, &data.uptime);
|
||||
do_output (s, sizeof (glibtop_uptime), &data.uptime);
|
||||
do_output (s, 0, NULL);
|
||||
glibtop_get_uptime_l (&server, &response.u.data.uptime);
|
||||
do_output (s, &response, 0, NULL);
|
||||
break;
|
||||
case GLIBTOP_CMND_LOADAVG:
|
||||
glibtop_get_loadavg_l (&server, &data.loadavg);
|
||||
do_output (s, sizeof (glibtop_loadavg), &data.loadavg);
|
||||
do_output (s, 0, NULL);
|
||||
glibtop_get_loadavg_l (&server, &response.u.data.loadavg);
|
||||
do_output (s, &response, 0, NULL);
|
||||
break;
|
||||
case GLIBTOP_CMND_SHM_LIMITS:
|
||||
glibtop_get_shm_limits_l (&server, &data.shm_limits);
|
||||
do_output (s, sizeof (glibtop_shm_limits), &data.shm_limits);
|
||||
do_output (s, 0, NULL);
|
||||
glibtop_get_shm_limits_l (&server, &response.u.data.shm_limits);
|
||||
do_output (s, &response, 0, NULL);
|
||||
break;
|
||||
case GLIBTOP_CMND_MSG_LIMITS:
|
||||
glibtop_get_msg_limits_l (&server, &data.msg_limits);
|
||||
do_output (s, sizeof (glibtop_msg_limits), &data.msg_limits);
|
||||
do_output (s, 0, NULL);
|
||||
glibtop_get_msg_limits_l (&server, &response.u.data.msg_limits);
|
||||
do_output (s, &response, 0, NULL);
|
||||
break;
|
||||
case GLIBTOP_CMND_SEM_LIMITS:
|
||||
glibtop_get_sem_limits_l (&server, &data.sem_limits);
|
||||
do_output (s, sizeof (glibtop_sem_limits), &data.sem_limits);
|
||||
do_output (s, 0, NULL);
|
||||
glibtop_get_sem_limits_l (&server, &response.u.data.sem_limits);
|
||||
do_output (s, &response, 0, NULL);
|
||||
break;
|
||||
case GLIBTOP_CMND_PROCLIST:
|
||||
ptr = glibtop_get_proclist_l (&server, &data.proclist);
|
||||
do_output (s, sizeof (glibtop_proclist), &data.proclist);
|
||||
do_output (s, data.proclist.total, ptr);
|
||||
ptr = glibtop_get_proclist_l (&server, &response.u.data.proclist);
|
||||
do_output (s, &response, response.u.data.proclist.total, ptr);
|
||||
glibtop_free_r (&server, ptr);
|
||||
break;
|
||||
case GLIBTOP_CMND_PROC_STATE:
|
||||
memcpy (&pid, parameter, sizeof (pid_t));
|
||||
glibtop_get_proc_state_l
|
||||
(&server, &data.proc_state, pid);
|
||||
do_output (s, sizeof (glibtop_proc_state), &data.proc_state);
|
||||
do_output (s, 0, NULL);
|
||||
(&server, &response.u.data.proc_state, pid);
|
||||
do_output (s, &response, 0, NULL);
|
||||
break;
|
||||
case GLIBTOP_CMND_PROC_UID:
|
||||
memcpy (&pid, parameter, sizeof (pid_t));
|
||||
glibtop_get_proc_uid_l
|
||||
(&server, &data.proc_uid, pid);
|
||||
do_output (s, sizeof (glibtop_proc_uid), &data.proc_uid);
|
||||
do_output (s, 0, NULL);
|
||||
(&server, &response.u.data.proc_uid, pid);
|
||||
do_output (s, &response, 0, NULL);
|
||||
break;
|
||||
case GLIBTOP_CMND_PROC_MEM:
|
||||
memcpy (&pid, parameter, sizeof (pid_t));
|
||||
glibtop_get_proc_mem_l
|
||||
(&server, &data.proc_mem, pid);
|
||||
do_output (s, sizeof (glibtop_proc_mem), &data.proc_mem);
|
||||
do_output (s, 0, NULL);
|
||||
(&server, &response.u.data.proc_mem, pid);
|
||||
do_output (s, &response, 0, NULL);
|
||||
break;
|
||||
case GLIBTOP_CMND_PROC_TIME:
|
||||
memcpy (&pid, parameter, sizeof (pid_t));
|
||||
glibtop_get_proc_time_l
|
||||
(&server, &data.proc_time, pid);
|
||||
do_output (s, sizeof (glibtop_proc_time), &data.proc_time);
|
||||
do_output (s, 0, NULL);
|
||||
(&server, &response.u.data.proc_time, pid);
|
||||
do_output (s, &response, 0, NULL);
|
||||
break;
|
||||
case GLIBTOP_CMND_PROC_SIGNAL:
|
||||
memcpy (&pid, parameter, sizeof (pid_t));
|
||||
glibtop_get_proc_signal_l
|
||||
(&server, &data.proc_signal, pid);
|
||||
do_output (s, sizeof (glibtop_proc_signal), &data.proc_signal);
|
||||
do_output (s, 0, NULL);
|
||||
(&server, &response.u.data.proc_signal, pid);
|
||||
do_output (s, &response, 0, NULL);
|
||||
break;
|
||||
case GLIBTOP_CMND_PROC_KERNEL:
|
||||
memcpy (&pid, parameter, sizeof (pid_t));
|
||||
glibtop_get_proc_kernel_l
|
||||
(&server, &data.proc_kernel, pid);
|
||||
do_output (s, sizeof (glibtop_proc_kernel), &data.proc_kernel);
|
||||
do_output (s, 0, NULL);
|
||||
(&server, &response.u.data.proc_kernel, pid);
|
||||
do_output (s, &response, 0, NULL);
|
||||
break;
|
||||
case GLIBTOP_CMND_PROC_SEGMENT:
|
||||
memcpy (&pid, parameter, sizeof (pid_t));
|
||||
glibtop_get_proc_segment_l
|
||||
(&server, &data.proc_segment, pid);
|
||||
do_output (s, sizeof (glibtop_proc_segment), &data.proc_segment);
|
||||
do_output (s, 0, NULL);
|
||||
(&server, &response.u.data.proc_segment, pid);
|
||||
do_output (s, &response, 0, NULL);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user