Skip to content

Commit 6468c61

Browse files
committed
FS parsing with review comments by Serge.
Signed-off-by: Anjali Kulkarni <[email protected]>
1 parent b9b2572 commit 6468c61

File tree

6 files changed

+138
-2
lines changed

6 files changed

+138
-2
lines changed

Makefile

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
CC = gcc
22
CFLAGS = -g -Wall -Werror -fPIC -std=gnu99
3-
DEPS = resource.h resource_impl.h resmem.h resnet.h resproc.h resvm.h rescpu.h
4-
OBJ = resource.o resmem.o resnet.o resproc.o reskern.o resvm.o rescpu.o
3+
DEPS = resource.h resource_impl.h resmem.h resnet.h resproc.h resvm.h rescpu.h resfs.h
4+
OBJ = resource.o resmem.o resnet.o resproc.o reskern.o resvm.o rescpu.o resfs.o
55
TEST = test
66
RM = rm -rf
77
CP = cp

resfs.c

+79
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
/* Copyright (C) 2023, Oracle and/or its affiliates. All rights reserved
2+
*
3+
* This file is part of libresource.
4+
*
5+
* libresource is free software: you can redistribute it and/or modify
6+
* it under the terms of the GNU Lesser General Public License as published by
7+
* the Free Software Foundation, either version 2 of the License, or
8+
* (at your option) any later version.
9+
*
10+
* libresource is distributed in the hope that it will be useful,
11+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
* GNU Lesser General Public License for more details.
14+
*
15+
* You should have received a copy of the GNU Lesser General Public License
16+
* along with libresource. If not, see <http://www.gnu.org/licenses/>.
17+
*/
18+
19+
#ifndef _RESOURCE_H
20+
#include "resource.h"
21+
#endif
22+
#include <stdio.h>
23+
#include <stdlib.h>
24+
#include <string.h>
25+
#include <unistd.h>
26+
#include "resfs.h"
27+
#include "resource_impl.h"
28+
#include <errno.h>
29+
#include <libgen.h>
30+
31+
int getfsinfo(int res_id, void *out, size_t sz, void **hint, int flags)
32+
{
33+
int ret;
34+
unsigned long long *fs;
35+
char buffer[512];
36+
37+
switch (res_id) {
38+
case FS_AIONR:
39+
ret = file_to_buf(AIONR, buffer, sizeof(buffer));
40+
if (ret <= 0)
41+
return -1;
42+
ret = sscanf(buffer, "%Lu", (unsigned long long *) out);
43+
if (ret != 1)
44+
return -1;
45+
break;
46+
case FS_AIOMAXNR:
47+
ret = file_to_buf(AIOMAXNR, buffer, sizeof(buffer));
48+
if (ret <= 0)
49+
return -1;
50+
ret = sscanf(buffer, "%Lu", (unsigned long long *) out);
51+
if (ret != 1)
52+
return -1;
53+
break;
54+
case FS_FILENR:
55+
fs = (unsigned long long *) out;
56+
ret = file_to_buf(FILENR, buffer, sizeof(buffer));
57+
if (ret <= 0)
58+
return -1;
59+
ret = sscanf(buffer, "%Lu %Lu %Lu", &fs[0], &fs[1], &fs[2]);
60+
if (ret != 3)
61+
return -1;
62+
break;
63+
case FS_FILEMAXNR:
64+
ret = file_to_buf(FILEMAXNR, buffer, sizeof(buffer));
65+
if (ret <= 0)
66+
return -1;
67+
ret = sscanf(buffer, "%Lu", (unsigned long long *) out);
68+
if (ret != 1)
69+
return -1;
70+
break;
71+
default:
72+
eprintf("Resource Id is invalid");
73+
errno = EINVAL;
74+
return -1;
75+
}
76+
return 0;
77+
78+
}
79+

resfs.h

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/* Copyright (C) 2023, Oracle and/or its affiliates. All rights reserved
2+
*
3+
* This file is part of libresource.
4+
*
5+
* libresource is free software: you can redistribute it and/or modify
6+
* it under the terms of the GNU Lesser General Public License as published by
7+
* the Free Software Foundation, either version 2 of the License, or
8+
* (at your option) any later version.
9+
*
10+
* libresource is distributed in the hope that it will be useful,
11+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
* GNU Lesser General Public License for more details.
14+
*
15+
* You should have received a copy of the GNU Lesser General Public License
16+
* along with libresource. If not, see <http://www.gnu.org/licenses/>.
17+
*/
18+
19+
#ifndef _RESFS_H
20+
#define _RESFS_H
21+
22+
#define AIONR "/proc/sys/fs/aio-nr"
23+
#define AIOMAXNR "/proc/sys/fs/aio-max-nr"
24+
#define FILENR "/proc/sys/fs/file-nr"
25+
#define FILEMAXNR "/proc/sys/fs/file-max"
26+
27+
extern int getfsinfo(int res_id, void *out, size_t sz, void **hint, int flags);
28+
#endif /* _RESFS_H */

resource.c

+4
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
#include "resproc.h"
3333
#include "resvm.h"
3434
#include "rescpu.h"
35+
#include "resfs.h"
3536

3637
/* Allocate memory for bulk resource information and initiate it
3738
* properly.
@@ -198,6 +199,9 @@ int res_read(int res_id, void *out, size_t out_sz, void **hint, int pid, int fla
198199

199200
if (res_id >= CPU_MIN && res_id < CPU_MAX)
200201
return getcpuinfo(res_id, out, out_sz, hint, flags);
202+
203+
if (res_id >= FS_MIN && res_id < FS_MAX)
204+
return getfsinfo(res_id, out, out_sz, hint, flags);
201205
return 0;
202206
}
203207

resource.h

+7
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,13 @@ typedef struct res_blk {
134134
#define RES_CPU_CORECOUNT 6002
135135
#define CPU_MAX 6010
136136

137+
#define FS_MIN 8000
138+
#define FS_AIONR 8001
139+
#define FS_AIOMAXNR 8002
140+
#define FS_FILENR 8003
141+
#define FS_FILEMAXNR 8004
142+
#define FS_MAX 8010
143+
137144
/* Structure to return RES_MEM_INFOALL resource information */
138145
typedef struct res_mem_infoall {
139146
size_t memfree;

test.c

+18
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,21 @@ void print_vmstat()
5252
printf("\n");
5353
}
5454

55+
void print_fs()
56+
{
57+
unsigned long long fs_value, val[3];
58+
59+
res_read(FS_AIONR, &fs_value, sizeof(fs_value), NULL, 0, 0);
60+
printf("FS_AIONR : %Lu\n",fs_value);
61+
res_read(FS_AIOMAXNR, &fs_value, sizeof(fs_value), NULL, 0, 0);
62+
printf("FS_AIOMAXNR : %Lu\n",fs_value);
63+
res_read(FS_FILENR, &val, sizeof(val), NULL, 0, 0);
64+
printf("FS_FILENR : %Lu %Lu %Lu\n",val[0], val[1], val[2]);
65+
res_read(FS_FILEMAXNR, &fs_value, sizeof(fs_value), NULL, 0, 0);
66+
printf("FS_FILEMAXNR : %Lu\n",fs_value);
67+
printf("\n");
68+
}
69+
5570
void print_cpuinfo()
5671
{
5772
struct cpuinfo *cpu;
@@ -81,4 +96,7 @@ int main(int argc, char **argv)
8196

8297
/* CPUINFO */
8398
print_cpuinfo();
99+
100+
/* FS */
101+
print_fs();
84102
}

0 commit comments

Comments
 (0)