Skip to content

Commit 06ecd5a

Browse files
committed
Smbios (Linux): improve performance of small file reading
1 parent 8fdaa98 commit 06ecd5a

2 files changed

Lines changed: 15 additions & 2 deletions

File tree

src/common/FFstrbuf.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,7 @@ static inline void ffStrbufInitMoveS(FFstrbuf* strbuf, char* heapStr)
230230
ffStrbufInitMoveNS(strbuf, (uint32_t) strlen(heapStr), heapStr);
231231
}
232232

233+
// Despite the name, this function resets strbuf to the initial/unallocated state
233234
static inline void ffStrbufDestroy(FFstrbuf* strbuf)
234235
{
235236
if(strbuf->allocated > 0)

src/common/impl/smbiosHelper.c

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -161,15 +161,27 @@ static bool parseSmbiosTable(const uint8_t* data, uint32_t length)
161161
#ifdef __linux__
162162
bool ffGetSmbiosValue(const char* devicesPath, const char* classPath, FFstrbuf* buffer)
163163
{
164-
if (ffReadFileBuffer(devicesPath, buffer))
164+
// /sys/class/dmi/id/* are all pseudo-files with very small content
165+
// so reading the whole file at once is efficient
166+
ffStrbufEnsureFixedLengthFree(buffer, 127);
167+
168+
ssize_t len = ffReadFileData(devicesPath, buffer->allocated - 1, buffer->chars);
169+
if (len > 0)
165170
{
171+
assert(len < buffer->allocated);
172+
buffer->chars[len] = '\0';
173+
buffer->length = (uint32_t) len;
166174
ffStrbufTrimRightSpace(buffer);
167175
if(ffIsSmbiosValueSet(buffer))
168176
return true;
169177
}
170178

171-
if (ffReadFileBuffer(classPath, buffer))
179+
len = ffReadFileData(classPath, buffer->allocated - 1, buffer->chars);
180+
if (len > 0)
172181
{
182+
assert(len < buffer->allocated);
183+
buffer->chars[len] = '\0';
184+
buffer->length = (uint32_t) len;
173185
ffStrbufTrimRightSpace(buffer);
174186
if(ffIsSmbiosValueSet(buffer))
175187
return true;

0 commit comments

Comments
 (0)