Skip to content
Snippets Groups Projects
Commit 7d89c06b authored by Fabio Roberto Vitello's avatar Fabio Roberto Vitello
Browse files

initial release

parents
No related branches found
No related tags found
No related merge requests found
Makefile 0 → 100644
# $Id: Makefile,v 1.1.1.1 2006/03/13 18:31:16 bino Exp $
# variables
PROGRAM=pbsacct
CC=c++
CFLAGS=-O -c
LFLAGS=-O
INSTALL_DIR=/usr/local/sbin
# make
VPATH=./
all: $(PROGRAM)
OBJECTS=main.o alloc.o read.o print.o
$(PROGRAM): $(OBJECTS)
$(CC) $(LFLAGS) -o $(PROGRAM) $(OBJECTS)
rm $(OBJECTS)
$(OBJECTS): main.h
$(OBJECTS): %.o: %.cpp
$(CC) $(CFLAGS) $<
clean:
rm -f $(PROGRAM) $(OBJECTS)
install:
cp $(PROGRAM) $(INSTALL_DIR)
cp mk_pbsacct $(INSTALL_DIR)
uninstall:
rm -f $(INSTALL_DIR)/$(PROGRAM)
rm -f $(INSTALL_DIR)/mk_pbsacct
//
// alloc.cpp
// pbsAccounting
//
// Created by Fabio Roberto Vitello on 16/06/17.
// Copyright © 2017 Fabio Roberto Vitello. All rights reserved.
//
#include "alloc.hpp"
/* $Id: alloc.c,v 1.1.1.1 2006/03/13 18:31:16 bino Exp $ */
#include <stdlib.h>
#include <string.h>
#include "main.h"
/* Allocation new group */
GROUP * new_group()
{
GROUP *ptr;
ptr = (GROUP *)malloc(sizeof(GROUP));
if (ptr != NULL) {
ptr->groupname[0] = '\0';
ptr->num_jobs = 0;
ptr->nodes = 0;
ptr->cpu_time = 0.0;
ptr->wall_time = 0.0;
ptr->all_cpu_time = 0.0;
ptr->all_wall_time = 0.0;
ptr->p_next = NULL;
return(ptr);
} else
return(NULL);
}
/* Allocation new user */
USER * new_user()
{
USER *ptr;
ptr = (USER *)malloc(sizeof(USER));
if (ptr != NULL) {
ptr->username[0] = '\0';
ptr->num_jobs = 0;
ptr->nodes = 0;
ptr->cpu_time = 0.0;
ptr->wall_time = 0.0;
ptr->all_cpu_time = 0.0;
ptr->all_wall_time = 0.0;
ptr->pg = NULL;
ptr->p_next = NULL;
return(ptr);
} else
return(NULL);
}
/* Clean groups */
void clean_groups(GROUP *ptr)
{
GROUP *ptr_aux;
while (ptr != NULL) {
ptr_aux = ptr;
ptr = ptr->p_next;
free(ptr_aux);
}
}
/* Clean users */
void clean_users(USER *ptr)
{
USER *ptr_aux;
while (ptr != NULL) {
ptr_aux = ptr;
ptr = ptr->p_next;
free(ptr_aux);
}
}
//
// alloc.hpp
// pbsAccounting
//
// Created by Fabio Roberto Vitello on 16/06/17.
// Copyright © 2017 Fabio Roberto Vitello. All rights reserved.
//
#ifndef alloc_hpp
#define alloc_hpp
#include <stdio.h>
#include "main.h"
typedef struct group GROUP;
typedef struct user USER;
void clean_groups(GROUP *ptr);
void clean_users(USER *ptr);
#endif /* alloc_hpp */
main.cpp 0 → 100644
/*
* $Id: pbsacct.c,v 1.1.1.1 2006/03/13 18:31:16 bino Exp $
*
* pbsacct - program to make summary and report for PBS system.
*
* written by: Albino Aveleda, bino@bino.eng.br
*
* options:
* -a : report users and groups
* -u : report users
* -g : report groups
* -b value : cost per hour
* -n opt : opt = [0..3]
* 0: value * cputime
* 1: value * walltime (default)
* 2: value * cputime * ncpus per job
* 3: value * walltime * ncpus per job
* -c : cputime information
* -s : statistic information
* -h : help
*/
#include <cstdlib>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include "main.h"
/* global variables */
GROUP *pgroups;
USER *pusers;
int f_user=0, f_group=0, f_bill=0, f_stat=0, f_cpu=0, f_stdin=0, opt=1;
char unit[4]={"SBU"};
double value;
int cpuvalue;
/* usage use */
void usage_exit(void)
{
fputs("usage: pbsacct -h\n", stderr);
exit(2);
}
/* Help */
void help()
{
puts("pbsacct: program to make summary and report for PBS system.");
puts("usage: pbsacct [-a][-u][-g][-b value][-n opt][-p value][-c][-s][-h] [file(s)]");
puts("\toptions:");
puts("\t-a : report per-user and per-group usage");
puts("\t-u : report per-user usage");
puts("\t-g : report per-group usage");
puts("\t-b value : cost per hour (working with -n options)");
puts("\t-n opt : select how the cost is calculate");
puts("\t opt = [0..3] where:");
puts("\t 0: value * cputime");
puts("\t 1: value * walltime (default)");
puts("\t 2: value * cputime * ncpus per job");
puts("\t 3: value * walltime * ncpus per job");
puts("\t 99: Custom Option added by Fabio Vitello to compute che cost for CHIPP in MUP Cluster");
puts("\t-c : include information about cputime");
puts("\t (if your operations system support)");
puts("\t-s : include statistic information");
puts("\t-p Custom Option added by Fabio Vitello: set the number of core to compute the to compute cputime in case of exclusive node request");
puts("\t-h : help");
puts("\tIf there isn't input file, it use the stdin.\n");
puts("\tExamples:");
puts("\t- Make a statistic report per-user usage on 20 and 21");
puts("\t days in October, 2002.");
puts("\t% pbsacct -u -s -p 24 -c 20021020 20021021\n");
puts("\t- Make a report from a linux cluster in October, 2002");
puts("\t with informations per-user and per-group, where the");
puts("\t cost per hour is SBU=10.00 per CPU.");
puts("\t% cat 200210* | pbsacct -a -p 24 -b 10.00 -n 3\n");
exit(0);
}
/* parse args */
void parse_args(int ac, char **av)
{
int options, err=0;
while ((options = getopt(ac, av, "augb:B:np:sch"))!=EOF)
switch(options) {
case 'a':
f_user = f_group = 1;
break;
case 'u':
f_user = 1;
break;
case 'g':
f_group = 1;
break;
case 'b':
f_bill = 1;
value = atof(optarg);
if (value < 0.0) err=1;
break;
case 'n':
opt = atoi(optarg);
//99 is custom option for MUP cluster customizatio, author Fabio Vitello
if ( ((opt < 0) && (opt > 3)) || (opt!=99) ) err=1;
break;
case 's':
f_stat = 1;
break;
case 'c':
f_cpu = 1;
break;
case 'p':
cpuvalue = atoi(optarg);
break;
case 'h':
help();
break;
default:
usage_exit();
break;
}
if (err) usage_exit();
}
/* Main program */
int main (int argc, char **argv)
{
int i;
char myname[MAX_HOSTNAME_SIZE + 1] = {"\0"};
/* check if there is argument */
parse_args(argc, argv);
/* check parameters */
if ((f_user == 0)&&(f_group == 0)){
fputs("Please, select per-user, per-group or both usage for make report.\n",stderr);
fputs("For help typing: ./pbsacct -h\n",stderr);
exit(2);
}
if (((opt == 0)||(opt == 2))&&(f_cpu == 0)){
fputs("Please, use '-c' option with '-n 0' and '-n 2'.\n",stderr);
exit(2);
}
/* get the hostname */
gethostname(myname, sizeof(myname));
/* initialize pointers */
pgroups = NULL;
pusers = NULL;
/* read file(s) */
if (optind < argc)
for (i=optind; i < argc; i++) {
/* printf("argv[%d]=%s\n",i,argv[i]); */
read_file(argv[i]);
} else {
f_stdin++;
read_file("\0");
}
/* print report */
print_report(myname);
/* clean memory */
clean_groups(pgroups);
clean_users(pusers);
}
main.h 0 → 100644
//
// main.h
// pbsAccounting
//
// Created by Fabio Roberto Vitello on 16/06/17.
// Copyright © 2017 Fabio Roberto Vitello. All rights reserved.
//
#ifndef main_h
#define main_h
#include "read.hpp"
#include "print.hpp"
#include "alloc.hpp"
/* $Id: pbsacct.h,v 1.1.1.1 2006/03/13 18:31:16 bino Exp $ */
#define MAX_HOSTNAME_SIZE 256 /* max length of hostname */
#define MAX_NAME_SIZE 20 /* max length of name */
typedef struct group GROUP;
typedef struct user USER;
struct group {
char groupname[MAX_NAME_SIZE];
int num_jobs;
int nodes;
double cpu_time;
double wall_time;
double all_cpu_time;
double all_wall_time;
GROUP *p_next;
};
struct user {
char username[MAX_NAME_SIZE];
int num_jobs;
int nodes;
double cpu_time;
double wall_time;
double all_cpu_time;
double all_wall_time;
GROUP *pg;
USER *p_next;
};
/* global variables */
extern GROUP *pgroups;
extern USER *pusers;
extern int f_user, f_group, f_bill, f_stat, f_cpu, f_stdin, opt;
extern char unit[4];
extern double value;
extern int cpuvalue;
/* functions */
extern GROUP * new_group();
extern USER * new_user();
#endif /* main_h */
print.cpp 0 → 100644
//
// print.cpp
// pbsAccounting
//
// Created by Fabio Roberto Vitello on 16/06/17.
// Copyright © 2017 Fabio Roberto Vitello. All rights reserved.
//
#include "print.hpp"
/* $Id: print.c,v 1.1.1.1 2006/03/13 18:31:16 bino Exp $ */
#include <stdio.h>
#include <string.h>
#include "main.h"
/* print report */
void print_report(char *hostname)
{
GROUP *pg;
USER *pu;
double total;
printf("\n*** Portable Batch System accounting ***\n");
printf("Server Name: %s\n", hostname);
/* print report user */
if (f_user) {
printf("\n*** PBS Per-User Usage Report ***\n");
printf("User Group #Jobs Wall(h)");
if (f_cpu) {
printf(" CPU(h)");
if (f_stat) printf(" Efcy(%)");
}
if (f_stat) {
printf(" Av.Wall(h)");
if (f_cpu) printf(" Av.CPU(h)");
printf(" Av.#CPU");
}
printf(" Sum Wall(h)");
if (f_cpu) printf(" Sum CPU(h)");
if (f_bill) printf("%14s",unit);
printf("\n");
pu = pusers;
while (pu != NULL){
printf("%-9s %-9s %5d %10.4f",pu->username,pu->pg->groupname,
pu->num_jobs,(pu->wall_time/60));
if (f_cpu) {
printf(" %10.4f",(pu->cpu_time/60));
if (f_stat) printf(" %6.2f",(pu->cpu_time/60)*100/(pu->wall_time/60));
}
if (f_stat) {
printf(" %10.4f",((pu->wall_time/60)/pu->num_jobs));
if (f_cpu) printf(" %10.4f",((pu->cpu_time/60)/pu->num_jobs));
printf(" %5.1f",(float)(pu->nodes/pu->num_jobs));
}
printf(" %13.4f",(pu->all_wall_time/60));
if (f_cpu) printf("%13.4f",(pu->all_cpu_time/60));
if (f_bill) {
switch(opt) {
case 0:
total = value * pu->cpu_time/60;
break;
case 1:
total = value * pu->wall_time/60;
break;
case 2:
total = value * pu->all_cpu_time/60;
break;
case 3:
total = value * pu->all_wall_time/60;
break;
case 99:
total = value * pu->all_wall_time/60;
break;
}
printf("%14.2f",total);
}
printf("\n");
pu = pu->p_next;
}
}
/* print report group */
if (f_group) {
printf("\n*** PBS Per-Group Usage Report ***\n");
printf("Group #Jobs Wall(h)");
if (f_cpu) {
printf(" CPU(h)");
if (f_stat) printf(" Efcy(%)");
}
if (f_stat) {
printf(" Av.Wall(h)");
if (f_cpu) printf(" Av.CPU(h)");
printf(" Av.#CPU");
}
printf(" Sum Wall(h)");
if (f_cpu) printf(" Sum CPU(h)");
if (f_bill) printf("%14s",unit);
printf("\n");
pg = pgroups;
while (pg != NULL){
printf("%-9s %5d %10.4f",pg->groupname,
pg->num_jobs,(pg->wall_time/60));
if (f_cpu) {
printf(" %10.4f",(pg->cpu_time/60));
if (f_stat) printf(" %6.2f",(pg->cpu_time/60)*100/(pg->wall_time/60));
}
if (f_stat) {
printf(" %10.4f",((pg->wall_time/60)/pg->num_jobs));
if (f_cpu) printf(" %10.4f",((pg->cpu_time/60)/pg->num_jobs));
printf(" %5.1f",(float)(pg->nodes/pg->num_jobs));
}
printf(" %13.4f",(pg->all_wall_time/60));
if (f_cpu) printf("%13.4f",(pg->all_cpu_time/60));
if (f_bill) {
switch(opt) {
case 0:
total = value * pg->cpu_time/60;
break;
case 1:
total = value * pg->wall_time/60;
break;
case 2:
total = value * pg->all_cpu_time/60;
break;
case 3:
total = value * pg->all_wall_time/60;
break;
}
printf("%14.2f",total);
}
printf("\n");
pg = pg->p_next;
}
}
}
//
// print.hpp
// pbsAccounting
//
// Created by Fabio Roberto Vitello on 16/06/17.
// Copyright © 2017 Fabio Roberto Vitello. All rights reserved.
//
#ifndef print_hpp
#define print_hpp
#include <stdio.h>
void print_report(char *hostname);
#endif /* print_hpp */
read.cpp 0 → 100644
//
// read.cpp
// pbsAccounting
//
// Created by Fabio Roberto Vitello on 16/06/17.
// Copyright © 2017 Fabio Roberto Vitello. All rights reserved.
//
#include "read.hpp"
/*
* $Id: read.c,v 1.1.1.1 2006/03/13 18:31:16 bino Exp $
* (based based on pbs_accounting.c from
* Albino Aveleda, NACAD-COPPE/UFRJ)
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "main.h"
/* read file(s) */
void read_file(char *file)
{
int len, nodes, hour, min, sec, test, linenum = 0;
double cpu_used, wall_used;
char buffer[BUFSIZ], *p, *ptr, *line;
char uid[MAX_NAME_SIZE], gid[MAX_NAME_SIZE];
GROUP *pg, *pg_aux;
USER *pu, *pu_aux;
FILE *f_in;
/* open PBS data file */
if (f_stdin)
f_in = stdin;
else
if ((f_in = fopen(file,"r")) == NULL) {
printf("\tCan't open file %s.\n",file);
exit(1);
}
/* read file */
while(fgets(buffer, BUFSIZ, f_in) != NULL) {
linenum++;
if (strstr(buffer, " end") != NULL) {
cpu_used = 0.0;
len = strlen(buffer) + 1;
line = static_cast<char*>(malloc(len * sizeof(char)));
strcpy(line, buffer);
ptr = strstr(line, "Resource_List.ncpus");
if (ptr == NULL) {
/* try node */
strcpy(line, buffer);
ptr = strstr(line, "Resource_List.nodect");
}
/* num of cpu or nodes */
if (ptr != NULL) {
p = strtok(ptr, "=");
p = strtok(NULL, "=");
nodes = atoi(p);
} else {
nodes = 1;
}
strcpy(line, buffer);
// printf("%s",line);
ptr = strstr(line, "Resource_List.place");
if (ptr != NULL) {
p = strtok(ptr, "=");
p = strtok(NULL, "=");
p = strtok(p, " ");
if (strcmp(p,"scatter:excl") == 0)
{
strcpy(line, buffer);
ptr = strstr(line, "Resource_List.nodect");
p = strtok(ptr, "=");
p = strtok(NULL, "=");
nodes = atoi(p)*cpuvalue;
// printf ("nodo esclusivo");
}
}
// printf("%d",nodes);
/* search for user name */
strcpy(line, buffer);
ptr = strstr(line, ";user");
if (ptr == NULL) {
fprintf(stderr,
"Warning: No user (id) data on line %d, skipping...\n",
linenum);
free(line);
continue;
}
p = strtok(ptr, "=");
p = strtok(NULL, "=");
sscanf(p, "%s ", uid);
p = strtok(NULL, "=");
sscanf(p, "%s ", gid);
strcpy(line, buffer);
ptr = strstr(line, "account");
if (ptr != NULL) {
p = strtok(ptr, "=");
p = strtok(NULL, "=");
sscanf(p, "%s ", gid);
}
/* search for cpu time */
if(f_cpu) {
strcpy(line, buffer);
ptr = strstr(line, "resources_used.cput");
if (ptr == NULL) {
fprintf(stderr,
"Warning: No used.cput data on line %d, skipping...\n",
linenum);
free(line);
continue;
}
p = strtok(ptr, "=");
p = strtok(NULL, "=");
sscanf(p, "%d:%d:%d ", &hour, &min, &sec);
cpu_used = (double)((double)sec/60) + min + (hour * 60);
/* cpu_used = (double)atof(p)/60; cpu minutes */
}
/* search for walltime */
strcpy(line, buffer);
ptr = strstr(line, "resources_used.walltime");
if (ptr == NULL) {
fprintf(stderr,
"Warning: No used.walltime data on line %d, skipping...\n",
linenum);
free(line);
continue;
}
p = strtok(ptr, "=");
p = strtok(NULL, "=");
sscanf(p, "%d:%d:%d ", &hour, &min, &sec);
/* walltime minutes */
wall_used = (double)((double)sec/60) + min + (hour * 60);
free(line);
/* lookup group */
if (pgroups == NULL){
pgroups = new_group();
pg = pgroups;
strcpy(pg->groupname,gid);
} else {
pg = pgroups;
test = strcmp(gid,pg->groupname);
if (test != 0) {
if (test < 0){
pgroups = new_group();
pgroups->p_next = pg;
pg = pgroups;
strcpy(pg->groupname,gid);
} else {
while ((pg->p_next != NULL) && (strcmp(gid,pg->p_next->groupname) >= 0))
pg = pg->p_next;
if (strcmp(gid, pg->groupname) > 0) {
if (pg->p_next == NULL){
pg->p_next = new_group();
} else {
pg_aux = new_group();
pg_aux->p_next = pg->p_next;
pg->p_next = pg_aux;
}
pg = pg->p_next;
strcpy(pg->groupname,gid);
}
}
}
}
pg->num_jobs++;
pg->nodes += nodes;
pg->cpu_time += cpu_used;
pg->wall_time += wall_used;
pg->all_cpu_time += nodes * cpu_used;
pg->all_wall_time += nodes * wall_used;
/* lookup user */
if (f_user) {
if (pusers == NULL){
pusers = new_user();
pu = pusers;
strcpy(pu->username,uid);
} else {
pu = pusers;
test = strcmp(uid,pu->username);
if (test != 0 ) {
if (test < 0) {
pusers = new_user();
pusers->p_next = pu;
pu = pusers;
strcpy(pu->username,uid);
} else {
while ((pu->p_next != NULL) && (strcmp(uid,pu->p_next->username) >= 0))
pu = pu->p_next;
if (strcmp(uid, pu->username) > 0) {
if (pu->p_next == NULL){
pu->p_next = new_user();
} else {
pu_aux = new_user();
pu_aux->p_next = pu->p_next;
pu->p_next = pu_aux;
}
pu = pu->p_next;
strcpy(pu->username,uid);
}
}
}
}
pu->num_jobs++;
pu->nodes += nodes;
pu->cpu_time += cpu_used;
pu->wall_time += wall_used;
pu->all_cpu_time += nodes * cpu_used;
pu->all_wall_time += nodes * wall_used;
pu->pg = pg;
}
}
}
/* close PBS data file */
if (!f_stdin) fclose(f_in);
}
read.hpp 0 → 100644
//
// read.hpp
// pbsAccounting
//
// Created by Fabio Roberto Vitello on 16/06/17.
// Copyright © 2017 Fabio Roberto Vitello. All rights reserved.
//
#ifndef read_hpp
#define read_hpp
#include <stdio.h>
void read_file(char *file);
#endif /* read_hpp */
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment