Skip to content

Commit

Permalink
Stats: Add a callback where applications can collect statistics data
Browse files Browse the repository at this point in the history
See examples/nfs-stats-cb.c for an example.

Signed-off-by: Ronnie Sahlberg <ronniesahlberg@gmail.com>
  • Loading branch information
sahlberg committed Aug 15, 2024
1 parent 89eb873 commit 572a81d
Show file tree
Hide file tree
Showing 8 changed files with 303 additions and 26 deletions.
1 change: 1 addition & 0 deletions examples/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ set(EXAMPLES nfsclient-async
nfs-io
nfs-ln
nfs-writefile
nfs-stats-cb
portmap-client)

if (ENABLE_MULTITHREADING)
Expand Down
3 changes: 2 additions & 1 deletion examples/Makefile.am
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
noinst_PROGRAMS = nfsclient-async nfsclient-raw nfsclient-sync \
nfsclient-bcast nfsclient-listservers nfs-fh nfs-io nfs-ln nfs4-cat \
portmap-client portmap-server nfs-writefile
portmap-client portmap-server nfs-writefile nfs-stats-cb

if HAVE_TALLOC_TEVENT
noinst_PROGRAMS += nfs4-cat-talloc
Expand Down Expand Up @@ -41,3 +41,4 @@ nfs_pthreads_example_LDADD = $(COMMON_LIBS)
nfs_pthreads_fstat_LDADD = $(COMMON_LIBS)
nfs_pthreads_writefile_LDADD = $(COMMON_LIBS)
nfs_pthreads_async_writefile_LDADD = $(COMMON_LIBS)
nfs_stats_cb_LDADD = $(COMMON_LIBS)
210 changes: 210 additions & 0 deletions examples/nfs-stats-cb.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,210 @@
/*
Copyright (C) by Ronnie Sahlberg <ronniesahlberg@gmail.com> 2015
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, see <http://www.gnu.org/licenses/>.
*/

#define _FILE_OFFSET_BITS 64
#define _GNU_SOURCE

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#ifdef AROS
#include "aros_compat.h"
#endif


#ifdef WIN32
#include <win32/win32_compat.h>
#pragma comment(lib, "ws2_32.lib")
WSADATA wsaData;
#else
#include <sys/stat.h>
#include <string.h>
#endif

#ifdef HAVE_POLL_H
#include <poll.h>
#endif

#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif

#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <sys/types.h>
#include <fcntl.h>
#include "libnfs.h"
#include "libnfs-raw.h"
#include "libnfs-raw-mount.h"

struct file_context {
int fd;
struct nfs_context *nfs;
struct nfsfh *nfsfh;
struct nfs_url *url;
};

void usage(void)
{
fprintf(stderr, "Usage: nfs-stats-cb <file>\n");
fprintf(stderr, "<file> read a file and print the stats callback data.\n");
exit(0);
}

static void
free_file_context(struct file_context *file_context)
{
if (file_context->fd != -1) {
close(file_context->fd);
}
if (file_context->nfsfh != NULL) {
nfs_close(file_context->nfs, file_context->nfsfh);
}
if (file_context->nfs != NULL) {
nfs_destroy_context(file_context->nfs);
}
nfs_destroy_url(file_context->url);
free(file_context);
}

void stats_cb(struct rpc_context *rpc,
struct rpc_stats_cb_data *data, void *private_data)
{
printf("%s XID:%08x %d/%d/%d Size:%d Status:%d %dms\n",
data->direction == CALL ? "CALL" : "REPLY",
data->xid,
data->prog, data->vers, data->proc,
data->size, data->status, data->response_time);
}

static struct file_context *
open_file(const char *url, int flags)
{
struct file_context *file_context;

file_context = malloc(sizeof(struct file_context));
if (file_context == NULL) {
fprintf(stderr, "Failed to malloc file_context\n");
return NULL;
}
file_context->fd = -1;
file_context->nfs = NULL;
file_context->nfsfh = NULL;
file_context->url = NULL;

file_context->nfs = nfs_init_context();
if (file_context->nfs == NULL) {
fprintf(stderr, "failed to init context\n");
free_file_context(file_context);
return NULL;
}

file_context->url = nfs_parse_url_full(file_context->nfs, url);
if (file_context->url == NULL) {
fprintf(stderr, "%s\n", nfs_get_error(file_context->nfs));
free_file_context(file_context);
return NULL;
}

if (nfs_mount(file_context->nfs, file_context->url->server,
file_context->url->path) != 0) {
fprintf(stderr, "Failed to mount nfs share : %s\n",
nfs_get_error(file_context->nfs));
free_file_context(file_context);
return NULL;
}
rpc_set_stats_cb(nfs_get_rpc_context(file_context->nfs), stats_cb, NULL);

if (flags == O_RDONLY) {
if (nfs_open(file_context->nfs, file_context->url->file, flags,
&file_context->nfsfh) != 0) {
fprintf(stderr, "Failed to open file %s: %s\n",
file_context->url->file,
nfs_get_error(file_context->nfs));
free_file_context(file_context);
return NULL;
}
} else {
if (nfs_creat(file_context->nfs, file_context->url->file, 0660,
&file_context->nfsfh) != 0) {
fprintf(stderr, "Failed to creat file %s: %s\n",
file_context->url->file,
nfs_get_error(file_context->nfs));
free_file_context(file_context);
return NULL;
}
}
return file_context;
}


#define BUFSIZE 1024*1024
static char buf[BUFSIZE];

int main(int argc, char *argv[])
{
struct file_context *nf;
struct nfs_stat_64 st;
uint64_t off;
int64_t count;

#ifdef WIN32
if (WSAStartup(MAKEWORD(2,2), &wsaData) != 0) {
printf("Failed to start Winsock2\n");
return 10;
}
#endif

#ifdef AROS
aros_init_socket();
#endif

if (argc < 2) {
usage();
}

nf = open_file(argv[1], O_RDONLY);
if (nf == NULL) {
fprintf(stderr, "Failed to open %s\n", argv[1]);
exit(10);
}
if (nfs_fstat64(nf->nfs, nf->nfsfh, &st) < 0) {
fprintf(stderr, "Failed to stat %s\n", argv[1]);
exit(10);
}

off = 0;
while (off < st.nfs_size) {
count = st.nfs_size - off;
if (count > BUFSIZE) {
count = BUFSIZE;
}
count = nfs_pread(nf->nfs, nf->nfsfh, buf, count, off);
if (count < 0) {
fprintf(stderr, "Failed to read from file\n");
free_file_context(nf);
return 10;
}
off += count;
}

free_file_context(nf);

return 0;
}
9 changes: 8 additions & 1 deletion include/libnfs-private.h
Original file line number Diff line number Diff line change
Expand Up @@ -419,8 +419,12 @@ struct rpc_context {

/* Per-transport RPC stats */
struct rpc_stats stats;
};

/* Stats callback */
rpc_stats_cb stats_cb;
void *stats_private_data;
};

struct rpc_pdu {
struct rpc_pdu *next;

Expand Down Expand Up @@ -535,6 +539,9 @@ struct rpc_pdu {
/* Set by rpc_allocate_pdu2() when we use AUTH_TLS for a NULL RPC request */
bool_t expect_starttls;
#endif
/* ONC-RPC header for outgoing PDUs */
struct rpc_msg msg;
uint64_t timestamp; /* only valid if a stats_cb has been set */
};

void rpc_reset_queue(struct rpc_queue *q);
Expand Down
25 changes: 25 additions & 0 deletions include/nfsc/libnfs-raw.h
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,31 @@ struct rpc_context;
EXTERN struct rpc_context *rpc_init_context(void);
EXTERN void rpc_destroy_context(struct rpc_context *rpc);

/*
* Stats callback for all ASYNC rpc functions
*/
struct rpc_pdu;
struct rpc_stats_cb_data {
uint32_t size;
uint32_t xid;
uint32_t direction;
uint32_t status; /* only valid in replies */
uint32_t prog;
uint32_t vers;
uint32_t proc;
uint64_t response_time; /* only valid in replies */
};
typedef void (*rpc_stats_cb)(struct rpc_context *rpc,
struct rpc_stats_cb_data *data,
void *private_data);
/*
* The callback executes in the context of the event-loop so it is vital
* that the callback will never block and will return as fast as possible.
*/
EXTERN void rpc_set_stats_cb(struct rpc_context *rpc, rpc_stats_cb cb,
void *private_data);


/*
* Commands that are in flight are kept on linked lists and keyed by
* XID so that responses received can be matched with a request.
Expand Down
1 change: 0 additions & 1 deletion include/nfsc/libnfs.h
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,6 @@ typedef void (*rpc_cb)(struct rpc_context *rpc, int status, void *data,
void *private_data);



/*
* NFS CONTEXT.
*/
Expand Down
7 changes: 7 additions & 0 deletions lib/libnfs.c
Original file line number Diff line number Diff line change
Expand Up @@ -2588,3 +2588,10 @@ rpc_null_task_authtls(struct rpc_context *rpc, int nfs_version, rpc_cb cb,
return pdu;
}
#endif /* HAVE_TLS */

void rpc_set_stats_cb(struct rpc_context *rpc, rpc_stats_cb cb,
void *private_data)
{
rpc->stats_cb = cb;
rpc->stats_private_data = private_data;
}
Loading

0 comments on commit 572a81d

Please sign in to comment.