Whamcloud - gitweb
b=16424
[fs/lustre-release.git] / snmp / lustre-snmp-util.c
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  * GPL HEADER START
5  *
6  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License version 2 only,
10  * as published by the Free Software Foundation.
11  *
12  * This program is distributed in the hope that it will be useful, but
13  * WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * General Public License version 2 for more details (a copy is included
16  * in the LICENSE file that accompanied this code).
17  *
18  * You should have received a copy of the GNU General Public License
19  * version 2 along with this program; If not, see
20  * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
21  *
22  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
23  * CA 95054 USA or visit www.sun.com if you need additional information or
24  * have any questions.
25  *
26  * GPL HEADER END
27  */
28 /*
29  * Copyright  2008 Sun Microsystems, Inc. All rights reserved
30  * Use is subject to license terms.
31  */
32 /*
33  * This file is part of Lustre, http://www.lustre.org/
34  * Lustre is a trademark of Sun Microsystems, Inc.
35  *
36  * snmp/lustre-snmp-util.c
37  *
38  * Author: PJ Kirner <pjkirner@clusterfs.com>
39  */
40
41 /*
42  *   include important headers
43  */
44
45 #include <net-snmp/net-snmp-config.h>
46 #include <net-snmp/net-snmp-includes.h>
47 #include <net-snmp/agent/net-snmp-agent-includes.h>
48
49 /*
50  *  include our .h file
51  */ 
52
53 #include <sys/types.h>
54 #if defined (__linux__)
55 #include <sys/vfs.h>
56 #endif
57 #include <dirent.h>
58 #include <sys/stat.h>
59 #include <unistd.h>
60 #include <stdio.h>
61 #include <stdarg.h>
62 #include <string.h>
63 #include "lustre-snmp-util.h"
64
65 /*********************************************************************
66  * Function:    get_file_list
67  *
68  * Description: For the given valid directory  path, returns the list
69  *              all directories or files in that path.
70  *
71  * Input:   'dirname' the directory path.
72  *          'file_type' if this takes the value DIR_TYPE then
73  *              returns the list of directories in that path.
74  *          If its of type FILE_TYPE then returns the list of files
75  *          in that path.
76  *          'count' pointer to number of elements returned in the
77  *          return string. 
78  *
79  * Output:  List of  directories/files in that path.
80  *
81  *********************************************************************/
82
83 char *get_file_list(const char *dirname, int file_type, uint32_t *count)
84 {
85
86     DIR           *pdir = NULL;
87     struct dirent *pdirent = NULL;
88     int           curr_offset = 0;
89     int           byte_count = 0;
90     int           file_count = 0;
91     char          *ret_str = NULL;
92     char          filename[MAX_PATH_SIZE];
93     int           cond1, cond2;
94
95     if ((dirname == NULL) || ((pdir = opendir(dirname)) == NULL )) {
96         if (dirname == NULL) {
97             report("%s %s:line %d %s", __FILE__, __FUNCTION__, __LINE__,
98                    "NULL directory is passed as parameter to funtion");
99         } else {
100             report("%s %s:line %d Error in opening the dir %s", __FILE__,
101                    __FUNCTION__, __LINE__, dirname);
102         }
103         if (count)
104             *count = 0;
105         return NULL;
106     }
107
108     while (1) {
109         if ((pdirent = readdir(pdir)) == NULL)
110             break;
111
112         /* Skip over '.' and '..' directores */
113         if ((pdirent->d_name[0] == '.') ||
114             !strcmp(pdirent->d_name, FILENAME_NUM_REF))
115             continue;
116         
117         sprintf(filename, "%s/%s", dirname, pdirent->d_name);
118         cond1 = (file_type == FILE_TYPE) && is_directory(filename);
119         cond2 = (file_type == DIR_TYPE) && (!is_directory(filename));
120
121         if (cond1 || cond2)
122             continue;
123
124         /* Calculate the number of bytes for this new entry.*/                    
125         byte_count += strlen(pdirent->d_name) + 1;
126         file_count++;
127     }
128     if (count)
129         *count = file_count;
130     
131     if (file_count != 0) {
132         
133         /* need one extra one for the finall NULL terminator*/
134         if ((ret_str = (char *) malloc(byte_count + 1)) == NULL) {
135             report("get_file_list() failed to malloc(%d)",byte_count+1);
136             closedir(pdir);
137             return NULL;
138         }    
139         
140         rewinddir(pdir);
141         
142         while (file_count != 0) {
143             if ((pdirent = readdir(pdir)) == NULL)
144                 break;
145
146             if ((pdirent->d_name[0] == '.') ||
147                 !strcmp(pdirent->d_name, FILENAME_NUM_REF))
148                 continue;
149             
150             sprintf(filename, "%s/%s", dirname, pdirent->d_name);
151             cond1 = (file_type == FILE_TYPE) && is_directory(filename);
152             cond2 = (file_type == DIR_TYPE) && (!is_directory(filename));
153
154             if (cond1 || cond2)
155                 continue;
156
157             strcpy(ret_str + curr_offset, pdirent->d_name);
158             curr_offset = curr_offset + strlen(pdirent->d_name) + 1;
159             file_count--;
160         }
161         /* Put in the finall null terminator*/
162         ret_str[byte_count] = '\0';
163     }
164     closedir(pdir);
165     return ret_str;
166 }
167
168
169 /*********************************************************************
170  * Function:    is_directory
171  *
172  * Description: Checks if given filename is a directory or not.
173  *              all directories or files in that path.
174  *
175  * Input:   'filename' the directory path to be checked.
176  *
177  * Output:  Returns 1 if its a directory else 0.
178  *
179  *********************************************************************/
180
181 int is_directory(const char *filename)
182 {
183
184     struct stat statf;
185     int result;
186
187     result = stat(filename, &statf);
188     return ((result == SUCCESS) && (statf.st_mode & S_IFDIR));
189 }
190
191 /*********************************************************************
192  * Function:    read_string
193  *
194  * Description: For the given valid file path, reads the data in
195  *              that file.
196  *
197  * Input:   'filepath' the file whose data is to be accessed.
198  *          'lustre_var' the data from the file is read into
199  *           this variable, returned to the requestor.
200  *          'var_max_size' the max size of the string
201  *          'report_error' boolean if error should be reported on 
202  *           missing filepath
203  *
204  * Output:  Returns SUCCESS if read successfully from file else
205  *          returns ERROR.
206  *********************************************************************/
207  
208 int  read_string(const char *filepath, char *lustre_var, size_t var_max_size)
209 {
210     FILE    *fptr = NULL;
211     int     len = 0;
212     int     ret_val = SUCCESS;
213     int     report_error = 1;
214
215     if ((filepath == NULL) || (lustre_var == NULL)) {
216         report("%s %s:line %d %s", __FILE__, __FUNCTION__, __LINE__,
217                "Input parameter is NULL");
218         ret_val = ERROR;
219     } else {
220         fptr = fopen(filepath, "r");
221
222         if (fptr == NULL) {
223             if(report_error)
224                 report("%s %s:line %d Unable to open the file %s", __FILE__,
225                        __FUNCTION__, __LINE__, filepath);
226             ret_val = ERROR;
227         } else {
228             if (fgets(lustre_var, var_max_size, fptr) == NULL) {
229                 report("%s %s:line %d read failed for file %s", __FILE__,
230                        __FUNCTION__, __LINE__, filepath);
231                  ret_val = ERROR;
232             } else {
233                 len = strlen(lustre_var);
234                 /*
235                     Last char is EOF, before string ends,
236                     so '\0' is moved to last but one.
237                 */
238                 lustre_var[len-1] = lustre_var[len];
239             }
240             fclose(fptr);
241         }
242     }
243     return ret_val;
244 }
245
246 /**************************************************************************
247  * Function:   lustrefs_ctrl
248  *
249  * Description: Execute /etc/init.d/lustre script for starting,
250  *              stopping and restarting Lustre services in child process.
251  *
252  * Input:  Start/Stop/Restart Command Number.
253  * Output: Returns  void
254  *
255  **************************************************************************/
256
257 void lustrefs_ctrl(int command)
258 {
259     char *cmd[3];
260
261     cmd[0] = LUSTRE_SERVICE;
262     switch (command) {
263     case ONLINE:
264         cmd[1] = "start";
265         break;
266     case OFFLINE:
267         cmd[1] = "stop";
268         break;
269     case RESTART:
270         cmd[1] = "restart";
271         break;
272     default:
273         return;
274     }
275
276     cmd[2] = (char *)0;
277
278     if (fork() == 0) {
279         execvp(cmd[0], cmd);
280         report("failed to execvp(\'%s %s\')",cmd[0],cmd[1]);
281     }
282     return;
283 }
284
285 /*****************************************************************************
286  * Function:     get_sysstatus
287  *
288  * Description:  Read /var/lustre/sysStatus file, and based on file contents
289  *               return the status of Lustre services.
290  *
291  * Input:   void
292  * Output:  Return ONLINE/OFFLINE/ONLINE PENDING/OFFLINE PENDING status
293  *          values.
294  *
295  ****************************************************************************/
296
297 int get_sysstatus(void)
298 {
299     FILE    *fptr = NULL;
300     int     len = 0;
301     int     ret_val = ERROR ;
302     char    sys_status[50] = {0};
303     
304     if(SUCCESS == read_string(FILENAME_SYS_STATUS,sys_status,sizeof(sys_status)))
305     {
306         if (memcmp(sys_status, STR_ONLINE_PENDING,strlen(STR_ONLINE_PENDING)) == 0)
307             ret_val = ONLINE_PENDING;
308         else if (memcmp(sys_status, STR_ONLINE, strlen(STR_ONLINE)) == 0)
309             ret_val = ONLINE;
310         else if (memcmp(sys_status, STR_OFFLINE_PENDING,strlen(STR_OFFLINE_PENDING)) == 0)
311             ret_val = OFFLINE_PENDING;
312         else if (memcmp(sys_status, STR_OFFLINE, strlen(STR_OFFLINE)) == 0)
313             ret_val = OFFLINE;
314         else
315             report("%s %s:line %d Bad Contents in file %s \'%s\'", __FILE__,
316                 __FUNCTION__, __LINE__, FILENAME_SYS_STATUS,sys_status);
317     }
318     return ret_val;
319 }
320
321
322 /*****************************************************************************
323  * Function:     read_ulong
324  *
325  * Description:  Read long values from lproc and copy to the location
326  *               pointed by input parameter.
327  *
328  * Input:   file path, and pointer for data to be copied
329  *
330  * Output:  Return ERROR or SUCCESS.
331  *
332  ****************************************************************************/
333
334 int read_ulong(const char *file_path, unsigned long *valuep)
335 {
336     char    file_data[MAX_LINE_SIZE];
337     int     ret_val;
338
339     if ((ret_val = read_string(file_path, file_data,sizeof(file_data))) == SUCCESS){
340         *valuep = strtoul(file_data,NULL,10);
341     }
342     return ret_val;
343 }
344
345 /*****************************************************************************
346  * Function:     read_counter64
347  *
348  * Description:  Read counter64 values from lproc and copy to the location
349  *               pointed by input parameter.
350  *
351  * Input:   file path, and pointer for data to be copied
352  *
353  * Output:  Return ERROR or SUCCESS.
354  *
355  ****************************************************************************/
356
357 int read_counter64(const char *file_path, counter64 *c64,int factor)
358 {
359     char    file_data[MAX_LINE_SIZE];
360     int     ret_val;
361     unsigned long long tmp = 0;
362
363     if ((ret_val = read_string(file_path, file_data,sizeof(file_data))) == SUCCESS) {
364         tmp = atoll(file_data) * factor;
365         c64->low = (unsigned long) (0x0FFFFFFFF & tmp);
366         tmp >>= 32; /* Shift right by 4 bytes */
367         c64->high = (unsigned long) (0x0FFFFFFFF & tmp);
368     }
369     return ret_val;
370 }
371
372 /*****************************************************************************
373  * Function:     get_nth_entry_from_list
374  *
375  * Description:  Find the n'th entry from a null terminated list of string
376  *
377  * Input:   dir_list - the list
378  *          num - the number of elements in the list
379  *          index - the index we are looking for
380  *
381  * Output:  Return NULL on failure, or the string name on success.
382  *
383  ****************************************************************************/
384
385 const char *get_nth_entry_from_list(const char* dir_list,int num,int index)
386 {
387     int i;
388     int cur_ptr = 0;
389     for(i=0;i<num;i++){
390         
391         /* 
392          * if we've reached the end of the list for some reason
393          * because num was wrong then stop processing
394          */
395         if( *(dir_list+cur_ptr) == 0)
396             break;
397             
398         /* If we've found the right one */    
399         if( i == index )
400             return dir_list+cur_ptr;
401             
402         /* Move to the next one*/            
403         cur_ptr += strlen(dir_list + cur_ptr)+1;
404     }
405     return NULL;
406 }
407
408 /*****************************************************************************
409  * Function:    report
410  *
411  * Description: This function used to report error msg to stderr and log into
412  *    log file(default file:/var/log/snmpd.log) when agent is started with
413  *    debug option -Dlsnmpd
414  * Input:   format string and variable arguments.
415  * Output:  void
416  ****************************************************************************/
417
418 void report(const char *fmt, ...)
419 {
420     char buf[1024];
421
422     va_list arg_list;
423     va_start(arg_list, fmt);
424     vsprintf(buf, fmt, arg_list);
425     va_end(arg_list);
426
427     DEBUGMSGTL(("lsnmpd", "%s\n", buf));
428     fprintf(stderr, "%s\n", buf);
429     return;
430 }
431
432
433
434 /**************************************************************************
435  * Function:   oid_table_ulong_handler
436  *
437  * Description: Fetch a unsigned long from the given location.
438  *              Setup var_len, and return a pointer to the data.
439  *
440  * Input:  file_path, and var_len pointer
441  *
442  * Output: NULL on failure, or pointer to data
443  *
444  **************************************************************************/
445
446 unsigned char* 
447     oid_table_ulong_handler(
448         const char* file_path,
449         size_t  *var_len)
450 {
451     static unsigned long ulong_ret;
452     if (SUCCESS != read_ulong(file_path,&ulong_ret))
453         return NULL;
454     *var_len = sizeof(ulong_ret);
455     return  (unsigned char *) &ulong_ret;
456 }
457
458 /**************************************************************************
459  * Function:   oid_table_c64_handler
460  *
461  * Description: Fetch a counter64 from the given location.
462  *              Setup var_len, and return a pointer to the data.
463  *
464  * Input:  file_path, and var_len pointer
465  *
466  * Output: NULL on failure, or pointer to data
467  *
468  **************************************************************************/
469
470 unsigned char* oid_table_c64_handler(const char* file_path,size_t  *var_len)
471 {
472     static counter64 c64;
473     if (SUCCESS != read_counter64(file_path,&c64,1))
474         return NULL;
475     *var_len = sizeof(c64);
476     return (unsigned char *) &c64;
477 }
478
479 /**************************************************************************
480  * Function:   oid_table_c64_kb_handler
481  *
482  * Description: Fetch a counter64 from the given location.
483  *              Setup var_len, and return a pointer to the data.
484  *              Different than oid_table_c64_handler in that
485  *              the original value is multiplied by 1024 before converting
486  *              to a counter64.  (e.g. turn KB into a Byte scaled value)
487  *
488  * Input:  file_path, and var_len pointer
489  *
490  * Output: NULL on failure, or pointer to data
491  *
492  **************************************************************************/
493
494 unsigned char* oid_table_c64_kb_handler(const char* file_path,size_t  *var_len)
495 {
496     static counter64 c64;
497     /* scale by factor of 1024*/
498     if (SUCCESS != read_counter64(file_path,&c64,1024))
499         return NULL;
500     *var_len = sizeof(c64);
501     return (unsigned char *) &c64;
502 }
503
504 /**************************************************************************
505  * Function:   oid_table_obj_name_handler
506  *
507  * Description: Just copy the file_path and return as the output value.
508  *
509  * Input:  file_path, and var_len pointer
510  *
511  * Output: NULL on failure, or pointer to data
512  *
513  **************************************************************************/
514
515 unsigned char* 
516     oid_table_obj_name_handler(
517         const char* file_path,
518         size_t  *var_len)
519 {
520     static unsigned char string[SPRINT_MAX_LEN];
521     *var_len = strlen(file_path);
522     *var_len = MIN_LEN(*var_len, sizeof(string));
523     memcpy(string, file_path, *var_len);
524     return (unsigned char *) string;
525 }
526
527 /**************************************************************************
528  * Function:   oid_table_string_handler
529  *
530  * Description: Fetch a string from the given location.
531  *              Setup var_len, and return a pointer to the data.
532  *
533  * Input:  file_path, and var_len pointer
534  *
535  * Output: NULL on failure, or pointer to data
536  *
537  **************************************************************************/
538
539 unsigned char* 
540     oid_table_string_handler(
541         const char* file_path,
542         size_t  *var_len)
543 {
544     static unsigned char string[SPRINT_MAX_LEN];
545     if( SUCCESS != read_string(file_path, string,sizeof(string)))
546         return NULL;
547     *var_len = strlen(string);
548     return (unsigned char *) string;
549 }
550
551
552 /**************************************************************************
553  * Function:   oid_table_is_directory_handler
554  *
555  * Description: Determine if the file_path is a directory.  
556  *              Setup a boolean return value.
557  *              Setup var_len, and return a pointer to the data.
558  *
559  * Input:  file_path, and var_len pointer
560  *
561  * Output: NULL on failure, or pointer to data
562  *
563  **************************************************************************/
564
565 unsigned char* 
566     oid_table_is_directory_handler(
567         const char* file_path,
568         size_t *var_len)
569 {
570     static long long_ret;
571     long_ret =  is_directory(file_path);
572     *var_len = sizeof(long_ret);
573     return (unsigned char *) &long_ret;
574 }
575
576 /**************************************************************************
577  * Function:   var_genericTable
578  *
579  * Description: Handle Table driven OID processing
580  *
581  **************************************************************************/
582
583 unsigned char *
584 var_genericTable(struct variable *vp,
585             oid     *name,
586             size_t  *length,
587             int     exact,
588             size_t  *var_len,
589             WriteMethod **write_method,
590             const char *path,
591             struct oid_table *ptable)
592 {
593     char *dir_list;
594     uint32_t num;
595     int  deviceindex;
596     unsigned char *ret_val = NULL;
597     int i=0;
598     const char* obj_name;
599     
600     
601     /*
602      * Get the list of file.  If there are no elements
603      * return nothing
604      */
605     if( 0 == (dir_list = get_file_list(path, DIR_TYPE, &num)))
606         return NULL;
607
608     /*
609      * Setup the table
610      */
611     if (header_simple_table(vp,name,length,exact,var_len,write_method, num)
612                                                 == MATCH_FAILED )
613         goto cleanup_and_exit;
614
615     /*
616      * The number of the device we're looking at
617      */
618     deviceindex = name[*length - 1] - 1;
619
620     /*
621      * If we couldn't find this element
622      * something must have recently changed return
623      * nothing
624      */
625     if(deviceindex >= num){
626         report("deviceindex=%d exceeds number of elements=%d",deviceindex,num);
627         goto cleanup_and_exit;
628     }
629
630     /*
631      * Fetch the object name from the list
632      */
633     obj_name = get_nth_entry_from_list(dir_list,num,deviceindex);
634     if(obj_name == NULL){
635         /*
636          * Note this should never really happen because we check deviceindex >=num
637          * above.  And dir_list should be consitent with num
638          * but just in case...
639          */
640         report("object name not found in list",deviceindex,num);
641         goto cleanup_and_exit;
642     }
643
644     /*
645      * Find the matching magic - or the end of the list
646      */
647     while(ptable[i].magic != vp->magic && ptable[i].magic != 0)
648         i++;
649
650     /*
651      * If we didn't find a matching entry return
652      */
653     if(ptable[i].magic==0)
654         goto cleanup_and_exit;
655
656     /*
657      * If the name is NULL is a special case and 
658      * just just pass the obj_name as the file_path
659      * otherwise we create a file path from the given components
660      */
661     if(ptable[i].name != 0){
662         char file_path[MAX_PATH_SIZE];
663         sprintf(file_path, "%s%s/%s",path,obj_name,ptable[i].name);
664         ret_val =  ptable[i].fhandler(file_path,var_len);
665     }
666     else
667         ret_val =  ptable[i].fhandler(obj_name,var_len);
668
669 cleanup_and_exit:
670     free(dir_list);
671     return ret_val;
672 };
673
674 /**************************************************************************
675  * Function:   stats_values
676  *
677  * Description: Setup nb_sample, min, max, sum and sum_square stats values
678                 for name_value from filepath.
679  *
680  * Input:  filepath, name_value,
681  *         pointer to nb_sample, min, max, sum, sum_square
682  *
683  * Output: SUCCESS or ERROR on failure
684  *
685  **************************************************************************/
686 int stats_values(char * filepath,char * name_value, unsigned long long * nb_sample, unsigned long long * min, unsigned long long * max, unsigned long long * sum, unsigned long long * sum_square)
687 {
688   FILE * statfile;
689   char line[MAX_LINE_SIZE];
690   int nbReadValues = 0;
691
692   if( (statfile=fopen(filepath,"r")) == NULL) {
693     report("stats_value() failed to open %s",filepath);
694     return ERROR;
695   }
696 /*find the good line for name_value*/
697   do {
698     if( fgets(line,MAX_LINE_SIZE,statfile) == NULL ) {
699       report("stats_values() failed to find %s values in %s stat_file",name_value,statfile);
700       goto error_out;
701     }
702   } while ( strstr(line,name_value) == NULL );
703 /*get stats*/
704   if((nbReadValues=sscanf(line,"%*s %llu %*s %*s %llu %llu %llu %llu",nb_sample,min,max,sum,sum_square)) == 5) {
705     goto success_out;
706   } else if( nbReadValues == 1 && *nb_sample == 0) {
707     *min = *max = *sum = *sum_square = 0;
708     goto success_out;
709   } else {
710     report("stats_values() failed to read stats_values for %s value in %s stat_file",name_value,statfile);
711     goto error_out;
712   }
713
714 success_out :
715   fclose(statfile);
716   return SUCCESS;
717 error_out :
718   fclose(statfile);
719   return ERROR;
720 }
721
722 /**************************************************************************
723  * Function:   mds_stats_values
724  *
725  * Description: Setup nb_sample, min, max, sum and sum_square stats values
726                 for mds stats name_value .
727  *
728  * Input:  name_value,
729  *         pointer to nb_sample, min, max, sum, sum_square
730  *
731  * Output: SUCCESS or ERROR on failure
732  *
733  **************************************************************************/
734 extern int mds_stats_values(char * name_value, unsigned long long * nb_sample, unsigned long long * min, unsigned long long * max, unsigned long long * sum, unsigned long long * sum_square)
735 {
736   unsigned long long tmp_nb_sample=0,tmp_min=0,tmp_max=0,tmp_sum=0,tmp_sum_square=0;
737 /*we parse the three MDS stat files and sum values*/
738   if( stats_values(FILEPATH_MDS_SERVER_STATS,name_value,&tmp_nb_sample,&tmp_min,&tmp_max,&tmp_sum,&tmp_sum_square) == ERROR ) {
739     return ERROR;
740   } else {
741     *nb_sample=tmp_nb_sample;
742     *min=tmp_min;
743     *max=tmp_max;
744     *sum=tmp_sum;
745     *sum_square=tmp_sum_square;
746   }
747
748   if( stats_values(FILEPATH_MDS_SERVER_READPAGE_STATS,name_value,&tmp_nb_sample,&tmp_min,&tmp_max,&tmp_sum,&tmp_sum_square) == ERROR ) {
749     return ERROR;
750   } else {
751     *nb_sample += tmp_nb_sample;
752     *min += tmp_min;
753     *max += tmp_max;
754     *sum += tmp_sum;
755     *sum_square += tmp_sum_square;
756   }
757
758   if( stats_values(FILEPATH_MDS_SERVER_SETATTR_STATS,name_value,&tmp_nb_sample,&tmp_min,&tmp_max,&tmp_sum,&tmp_sum_square) == ERROR ) {
759     return ERROR;
760   } else {
761     *nb_sample += tmp_nb_sample;
762     *min += tmp_min;
763     *max += tmp_max;
764     *sum += tmp_sum;
765     *sum_square += tmp_sum_square;
766   }
767   
768   return SUCCESS;
769 }