summaryrefslogtreecommitdiff
path: root/src/pmdas/lustrecomm/refresh_file.c
diff options
context:
space:
mode:
authorIgor Pashev <pashev.igor@gmail.com>2014-10-26 12:33:50 +0400
committerIgor Pashev <pashev.igor@gmail.com>2014-10-26 12:33:50 +0400
commit47e6e7c84f008a53061e661f31ae96629bc694ef (patch)
tree648a07f3b5b9d67ce19b0fd72e8caa1175c98f1a /src/pmdas/lustrecomm/refresh_file.c
downloadpcp-debian.tar.gz
Debian 3.9.10debian/3.9.10debian
Diffstat (limited to 'src/pmdas/lustrecomm/refresh_file.c')
-rw-r--r--src/pmdas/lustrecomm/refresh_file.c85
1 files changed, 85 insertions, 0 deletions
diff --git a/src/pmdas/lustrecomm/refresh_file.c b/src/pmdas/lustrecomm/refresh_file.c
new file mode 100644
index 0000000..86ca16f
--- /dev/null
+++ b/src/pmdas/lustrecomm/refresh_file.c
@@ -0,0 +1,85 @@
+/*
+ * Lustre common /proc PMDA
+ *
+ * Copyright (c) 2008 Silicon Graphics, Inc. All Rights Reserved.
+ *
+ * Author: Scott Emery <emery@sgi.com>
+ *
+ * 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 2 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, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+
+
+#define FILE_TIME_OFFSET 1
+
+#include "libreadfiles.h"
+
+static struct timespec file_time_offset = { 0 , 900000000 };
+
+
+int refresh_file( struct file_state *f_s ){
+ struct timespec now, tmp;
+ int i = 0;
+
+ if (f_s->datap) {
+ /* get time */
+ if ( clock_gettime(CLOCK_MONOTONIC, &now) < 0 ) {
+ /* if we don't know what time it is */
+ /* there's nothing we can do with this */
+ return 0;
+ }
+ /* if time since last refresh > delta */
+ tmp = timespec_add( &f_s->ts, &file_time_offset);
+ if ( timespec_le( &now, &tmp) ) {
+ /* file is recent */
+ return 0;
+ }
+ f_s->ts = now;
+ /* clear old data, make errors obvious, autoterm trailing strings */
+ memset ( f_s->datap, 0, f_s->datas );
+ }
+ /* if fd is null open file */
+ if ( f_s->fd <= 0) {
+ if (( f_s->fd = open (f_s->filename, O_RDONLY)) < 0) {
+ perror("refresh_file: open");
+ return -1;
+ }
+ }
+ if ( lseek (f_s->fd, 0, SEEK_SET) < 0 ) {
+ perror("refresh_file: initial seek");
+ return -1;
+ }
+ /* only grow, never shrink... what would be the point? */
+ while (f_s->datap && (i = read (f_s->fd, f_s->datap, f_s->datas)) >= f_s->datas ){
+ /* oh heck, what do I do if this fails? */
+ f_s->datas += BUFFERBLOCK;
+ if ((f_s->datap = realloc(f_s->datap, f_s->datas)) == NULL){
+ free((char *)f_s->datap);
+ perror("refresh_file: realloc");
+ return -1;
+ }
+ if ( lseek (f_s->fd, 0, SEEK_SET) < 0 ) {
+ perror("refresh_file: subsequent seek");
+ return -1;
+ }
+ }
+ if (i < 0 ){
+ /* read failed */
+ perror("refresh_file: file read failed");
+ return -1;
+ }
+ return 0;
+}
+
+