Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
M
MojitOS
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
sepia-pub
MojitOS
Commits
079b2506
Commit
079b2506
authored
2 years ago
by
floreal.risso
Browse files
Options
Downloads
Patches
Plain Diff
format
parent
a7c3287a
No related branches found
No related tags found
2 merge requests
!9
fix sensor example (doc)
,
!7
Add memory counters and fix makefile
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
doc/info_reader_ex.c
+57
-28
57 additions, 28 deletions
doc/info_reader_ex.c
src/memory_counters.c
+95
-85
95 additions, 85 deletions
src/memory_counters.c
with
152 additions
and
113 deletions
doc/info_reader_ex.c
+
57
−
28
View file @
079b2506
...
...
@@ -18,8 +18,10 @@
*******************************************************/
// ~/mojitos/doc/$ gcc -Wall -Wextra -Wpedantic -O3 -o info_reader_ex
// info_reader_ex.c ./../src/util.c && ./info_reader_ex
// ~/mojitos/doc/$ gcc -Wall -Wextra -Wpedantic -O3 -o info_reader_ex info_reader_ex.c ./../src/util.c && .
/info_reader
_ex
#include
"./../lib
/info_reader
.c"
#include
"./../lib/info_reader.h"
#define MAX_PROCS 64
...
...
@@ -40,7 +42,7 @@ typedef struct {
GenericPointer
int_allocator
(
char
*
s
)
{
unsigned
int
value
=
atoi
(
s
);
return
(
GenericPointer
)
value
;
return
(
GenericPointer
)
value
;
}
// -- Define the behaviour if the attempted value is a string
...
...
@@ -48,49 +50,49 @@ GenericPointer string_allocator(char *s)
{
char
*
value
=
malloc
(
strlen
(
s
)
+
1
);
strcpy
(
value
,
s
);
return
(
GenericPointer
)
value
;
return
(
GenericPointer
)
value
;
}
// -- Define the processor setter
void
set_processor
(
GenericPointer
storage
,
GenericPointer
data
)
{
Cpu
*
cpu
=
(
Cpu
*
)
storage
;
Cpu
*
cpu
=
(
Cpu
*
)
storage
;
cpu
->
processor
=
data
;
}
// -- Define the vendor_id setter
void
set_vendor_id
(
GenericPointer
storage
,
GenericPointer
data
)
{
Cpu
*
cpu
=
(
Cpu
*
)
storage
;
cpu
->
vendor_id
=
(
char
*
)
data
;
Cpu
*
cpu
=
(
Cpu
*
)
storage
;
cpu
->
vendor_id
=
(
char
*
)
data
;
}
// -- Define the family setter
void
set_family
(
GenericPointer
storage
,
GenericPointer
data
)
{
Cpu
*
cpu
=
(
Cpu
*
)
storage
;
Cpu
*
cpu
=
(
Cpu
*
)
storage
;
cpu
->
family
=
data
;
}
// -- Define the core_id setter
void
set_core_id
(
GenericPointer
storage
,
GenericPointer
data
)
{
Cpu
*
cpu
=
(
Cpu
*
)
storage
;
Cpu
*
cpu
=
(
Cpu
*
)
storage
;
cpu
->
core_id
=
data
;
}
// -- Define the physical_id setter
void
set_physical_id
(
GenericPointer
storage
,
GenericPointer
data
)
{
Cpu
*
cpu
=
(
Cpu
*
)
storage
;
Cpu
*
cpu
=
(
Cpu
*
)
storage
;
cpu
->
physical_id
=
data
;
}
// -- Define the model_name setter
void
set_model_name
(
GenericPointer
storage
,
GenericPointer
data
)
{
Cpu
*
cpu
=
(
Cpu
*
)
storage
;
cpu
->
model_name
=
(
char
*
)
data
;
Cpu
*
cpu
=
(
Cpu
*
)
storage
;
cpu
->
model_name
=
(
char
*
)
data
;
}
int
main
()
...
...
@@ -98,26 +100,54 @@ int main()
Cpu
cpus
[
MAX_PROCS
];
// -- Define the setter, the allocator for each key / separator.
KeyFinder
keys
[]
=
{
{.
key
=
"processor"
,
.
delimiter
=
": "
,
.
copy
=
(
CopyAllocator
*
)
int_allocator
,
.
set
=
(
Setter
*
)
set_processor
},
{.
key
=
"vendor_id"
,
.
delimiter
=
": "
,
.
copy
=
(
CopyAllocator
*
)
string_allocator
,
.
set
=
(
Setter
*
)
set_vendor_id
},
{.
key
=
"cpu family"
,
.
delimiter
=
": "
,
.
copy
=
(
CopyAllocator
*
)
int_allocator
,
.
set
=
(
Setter
*
)
set_family
},
{.
key
=
"core id"
,
.
delimiter
=
": "
,
.
copy
=
(
CopyAllocator
*
)
int_allocator
,
.
set
=
(
Setter
*
)
set_core_id
},
{.
key
=
"physical id"
,
.
delimiter
=
": "
,
.
copy
=
(
CopyAllocator
*
)
int_allocator
,
.
set
=
(
Setter
*
)
set_physical_id
},
{.
key
=
"model name"
,
.
delimiter
=
": "
,
.
copy
=
(
CopyAllocator
*
)
string_allocator
,
.
set
=
(
Setter
*
)
set_model_name
}
KeyFinder
keys
[]
=
{{
.
key
=
"processor"
,
.
delimiter
=
": "
,
.
copy
=
(
CopyAllocator
*
)
int_allocator
,
.
set
=
(
Setter
*
)
set_processor
},
{
.
key
=
"vendor_id"
,
.
delimiter
=
": "
,
.
copy
=
(
CopyAllocator
*
)
string_allocator
,
.
set
=
(
Setter
*
)
set_vendor_id
},
{
.
key
=
"cpu family"
,
.
delimiter
=
": "
,
.
copy
=
(
CopyAllocator
*
)
int_allocator
,
.
set
=
(
Setter
*
)
set_family
},
{
.
key
=
"core id"
,
.
delimiter
=
": "
,
.
copy
=
(
CopyAllocator
*
)
int_allocator
,
.
set
=
(
Setter
*
)
set_core_id
},
{
.
key
=
"physical id"
,
.
delimiter
=
": "
,
.
copy
=
(
CopyAllocator
*
)
int_allocator
,
.
set
=
(
Setter
*
)
set_physical_id
},
{
.
key
=
"model name"
,
.
delimiter
=
": "
,
.
copy
=
(
CopyAllocator
*
)
string_allocator
,
.
set
=
(
Setter
*
)
set_model_name
}
};
size_t
nb_keys
=
sizeof
(
keys
)
/
sizeof
(
KeyFinder
);
size_t
nb_keys
=
sizeof
(
keys
)
/
sizeof
(
KeyFinder
);
// -- Init the parser
Parser
parser
=
{
.
storage
=
(
GenericPointer
)
cpus
,
.
capacity
=
MAX_PROCS
,
.
storage_struct_size
=
sizeof
(
Cpu
),
.
keys
=
keys
,
.
nb_keys
=
nb_keys
,
.
file
=
fopen
(
"/proc/cpuinfo"
,
"r"
)
};
Parser
parser
=
{.
storage
=
(
GenericPointer
)
cpus
,
.
capacity
=
MAX_PROCS
,
.
storage_struct_size
=
sizeof
(
Cpu
),
.
keys
=
keys
,
.
nb_keys
=
nb_keys
,
.
file
=
fopen
(
"/proc/cpuinfo"
,
"r"
)
};
// -- Parse the file
parse
(
&
parser
);
...
...
@@ -137,4 +167,3 @@ int main()
printf
(
"==============================
\n
"
);
return
0
;
}
This diff is collapsed.
Click to expand it.
src/memory_counters.c
+
95
−
85
View file @
079b2506
...
...
@@ -10,111 +10,121 @@
static
const
char
*
path
=
"/proc/meminfo"
;
typedef
struct
{
KeyFinder
*
keys
;
unsigned
int
count
;
FILE
*
file
;
KeyFinder
*
keys
;
unsigned
int
count
;
FILE
*
file
;
}
MemoryCounters
;
GenericPointer
long_allocator
(
char
*
s
)
{
long
value
=
atol
(
s
);
return
(
GenericPointer
)
value
;
GenericPointer
long_allocator
(
char
*
s
)
{
long
value
=
atol
(
s
);
return
(
GenericPointer
)
value
;
}
KeyFinder
*
build_keyfinder
(
unsigned
int
count
,
unsigned
int
*
indexes
)
{
KeyFinder
*
keys
=
(
KeyFinder
*
)
calloc
(
count
,
sizeof
(
KeyFinder
));
for
(
unsigned
int
i
=
0
;
i
<
count
;
i
++
)
{
unsigned
int
idx
=
indexes
[
i
];
KeyFinder
key
=
{.
key
=
memory_counters
[
idx
],
.
delimiter
=
":"
,
.
copy
=
long_allocator
,
.
set
=
setter_functions
[
i
]};
memcpy
(
&
keys
[
i
],
&
key
,
sizeof
(
KeyFinder
));
}
return
keys
;
KeyFinder
*
build_keyfinder
(
unsigned
int
count
,
unsigned
int
*
indexes
)
{
KeyFinder
*
keys
=
(
KeyFinder
*
)
calloc
(
count
,
sizeof
(
KeyFinder
));
for
(
unsigned
int
i
=
0
;
i
<
count
;
i
++
)
{
unsigned
int
idx
=
indexes
[
i
];
KeyFinder
key
=
{.
key
=
memory_counters
[
idx
],
.
delimiter
=
":"
,
.
copy
=
long_allocator
,
.
set
=
setter_functions
[
i
]
};
memcpy
(
&
keys
[
i
],
&
key
,
sizeof
(
KeyFinder
));
}
return
keys
;
}
void
memory_list
(
char
*
memory_string
,
unsigned
int
*
count
,
unsigned
int
*
indexes
)
{
char
*
token
;
*
count
=
0
;
while
((
token
=
strtok
(
memory_string
,
","
))
!=
NULL
)
{
memory_string
=
NULL
;
unsigned
int
i
;
for
(
i
=
0
;
i
<
NB_COUNTERS
;
i
++
)
{
if
(
strcmp
(
memory_counters
[
i
],
token
)
==
0
)
{
(
*
count
)
++
;
indexes
[
*
count
-
1
]
=
i
;
break
;
}
}
if
(
i
==
NB_COUNTERS
)
{
fprintf
(
stderr
,
"Unknown memory counter: %s
\n
"
,
token
);
exit
(
EXIT_FAILURE
);
unsigned
int
*
indexes
)
{
char
*
token
;
*
count
=
0
;
while
((
token
=
strtok
(
memory_string
,
","
))
!=
NULL
)
{
memory_string
=
NULL
;
unsigned
int
i
;
for
(
i
=
0
;
i
<
NB_COUNTERS
;
i
++
)
{
if
(
strcmp
(
memory_counters
[
i
],
token
)
==
0
)
{
(
*
count
)
++
;
indexes
[
*
count
-
1
]
=
i
;
break
;
}
}
if
(
i
==
NB_COUNTERS
)
{
fprintf
(
stderr
,
"Unknown memory counter: %s
\n
"
,
token
);
exit
(
EXIT_FAILURE
);
}
if
((
*
count
)
>
NB_COUNTERS
)
{
fprintf
(
stderr
,
"Too much counters, there are probably duplicates
\n
"
);
exit
(
EXIT_FAILURE
);
}
}
if
((
*
count
)
>
NB_COUNTERS
)
{
fprintf
(
stderr
,
"Too much counters, there are probably duplicates
\n
"
);
exit
(
EXIT_FAILURE
);
}
}
}
unsigned
int
init_memory_counters
(
char
*
args
,
void
**
ptr
)
{
unsigned
int
indexes
[
NB_COUNTERS
];
unsigned
int
count
=
0
;
memory_list
(
args
,
&
count
,
indexes
);
unsigned
int
init_memory_counters
(
char
*
args
,
void
**
ptr
)
{
unsigned
int
indexes
[
NB_COUNTERS
];
unsigned
int
count
=
0
;
memory_list
(
args
,
&
count
,
indexes
);
KeyFinder
*
keys
=
build_keyfinder
(
count
,
indexes
);
FILE
*
file
=
fopen
(
path
,
"r"
);
KeyFinder
*
keys
=
build_keyfinder
(
count
,
indexes
);
FILE
*
file
=
fopen
(
path
,
"r"
);
MemoryCounters
*
counters
=
calloc
(
1
,
sizeof
(
MemoryCounters
));
counters
->
keys
=
keys
;
counters
->
count
=
count
;
counters
->
file
=
file
;
MemoryCounters
*
counters
=
calloc
(
1
,
sizeof
(
MemoryCounters
));
counters
->
keys
=
keys
;
counters
->
count
=
count
;
counters
->
file
=
file
;
*
ptr
=
(
void
*
)
counters
;
return
count
;
*
ptr
=
(
void
*
)
counters
;
return
count
;
}
unsigned
int
get_memory_counters
(
uint64_t
*
results
,
void
*
ptr
)
{
MemoryCounters
*
counters
=
(
MemoryCounters
*
)
ptr
;
fseek
(
counters
->
file
,
0
,
SEEK_SET
);
Parser
parser
=
{.
storage
=
(
GenericPointer
)
results
,
.
capacity
=
1
,
.
nb_stored
=
0
,
.
storage_struct_size
=
sizeof
(
uint64_t
)
*
counters
->
count
,
.
keys
=
counters
->
keys
,
.
nb_keys
=
counters
->
count
,
.
file
=
counters
->
file
};
parse
(
&
parser
);
return
counters
->
count
;
unsigned
int
get_memory_counters
(
uint64_t
*
results
,
void
*
ptr
)
{
MemoryCounters
*
counters
=
(
MemoryCounters
*
)
ptr
;
fseek
(
counters
->
file
,
0
,
SEEK_SET
);
Parser
parser
=
{.
storage
=
(
GenericPointer
)
results
,
.
capacity
=
1
,
.
nb_stored
=
0
,
.
storage_struct_size
=
sizeof
(
uint64_t
)
*
counters
->
count
,
.
keys
=
counters
->
keys
,
.
nb_keys
=
counters
->
count
,
.
file
=
counters
->
file
};
parse
(
&
parser
);
return
counters
->
count
;
}
void
label_memory_counters
(
char
**
labels
,
void
*
ptr
)
{
MemoryCounters
*
counters
=
(
MemoryCounters
*
)
ptr
;
for
(
unsigned
int
i
=
0
;
i
<
counters
->
count
;
i
++
)
{
labels
[
i
]
=
counters
->
keys
[
i
].
key
;
}
void
label_memory_counters
(
char
**
labels
,
void
*
ptr
)
{
MemoryCounters
*
counters
=
(
MemoryCounters
*
)
ptr
;
for
(
unsigned
int
i
=
0
;
i
<
counters
->
count
;
i
++
)
{
labels
[
i
]
=
counters
->
keys
[
i
].
key
;
}
}
void
clean_memory_counters
(
void
*
ptr
)
{
MemoryCounters
*
counters
=
(
MemoryCounters
*
)
ptr
;
fclose
(
counters
->
file
);
free
(
counters
->
keys
);
free
(
ptr
);
void
clean_memory_counters
(
void
*
ptr
)
{
MemoryCounters
*
counters
=
(
MemoryCounters
*
)
ptr
;
fclose
(
counters
->
file
);
free
(
counters
->
keys
);
free
(
ptr
);
}
void
*
show_all_memory_counters
(
void
*
none1
,
size_t
none2
)
{
for
(
unsigned
int
i
=
0
;
i
<
NB_COUNTERS
;
i
++
)
{
printf
(
"%s
\n
"
,
memory_counters
[
i
]);
}
void
*
show_all_memory_counters
(
void
*
none1
,
size_t
none2
)
{
for
(
unsigned
int
i
=
0
;
i
<
NB_COUNTERS
;
i
++
)
{
printf
(
"%s
\n
"
,
memory_counters
[
i
]);
}
UNUSED
(
none1
);
UNUSED
(
none2
);
exit
(
EXIT_SUCCESS
);
return
NULL
;
/* not reached */
UNUSED
(
none1
);
UNUSED
(
none2
);
exit
(
EXIT_SUCCESS
);
return
NULL
;
/* not reached */
}
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment