[libvirt] [PATCH V3] util: Add more virsysfs functions for handling resctrl sysfs

Eli Qiao liyong.qiao at intel.com
Fri Mar 31 10:05:32 UTC 2017


Extended /sys/fs/resctrl sysfs handling such as:

    Read string/uint.
    Write string.
    Create/remove directory.

All these operations will be while we are enabled CAT feature later.

Signed-off-by: Eli Qiao <liyong.qiao at intel.com>
---
 src/libvirt_private.syms |   9 +++
 src/util/virsysfs.c      | 143 +++++++++++++++++++++++++++++++++++++++++++++++
 src/util/virsysfs.h      |  26 +++++++++
 src/util/virsysfspriv.h  |   1 +
 4 files changed, 179 insertions(+)

diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms
index b551cb8..f6644cb 100644
--- a/src/libvirt_private.syms
+++ b/src/libvirt_private.syms
@@ -2620,17 +2620,26 @@ virVasprintfInternal;


 # util/virsysfs.h
+virSysfsCreateResCtrlDir;
 virSysfsGetCpuValueBitmap;
 virSysfsGetCpuValueInt;
 virSysfsGetCpuValueString;
 virSysfsGetCpuValueUint;
 virSysfsGetNodeValueBitmap;
 virSysfsGetNodeValueString;
+virSysfsGetResctrlInfoString;
+virSysfsGetResctrlInfoUint;
+virSysfsGetResctrlPath;
+virSysfsGetResctrlString;
+virSysfsGetResctrlUint;
 virSysfsGetSystemPath;
 virSysfsGetValueBitmap;
 virSysfsGetValueInt;
 virSysfsGetValueString;
+virSysfsRemoveResCtrlDir;
+virSysfsSetResctrlPath;
 virSysfsSetSystemPath;
+virSysfsWriteResctrlString;


 # util/virsysinfo.h
diff --git a/src/util/virsysfs.c b/src/util/virsysfs.c
index 7a98b48..a17c391 100644
--- a/src/util/virsysfs.c
+++ b/src/util/virsysfs.c
@@ -19,6 +19,7 @@
  */

 #include <config.h>
+#include <fcntl.h>

 #include "internal.h"

@@ -36,8 +37,10 @@ VIR_LOG_INIT("util.sysfs");

 #define VIR_SYSFS_VALUE_MAXLEN 8192
 #define SYSFS_SYSTEM_PATH "/sys/devices/system"
+#define SYSFS_RESCTRL_PATH "/sys/fs/resctrl"

 static const char *sysfs_system_path = SYSFS_SYSTEM_PATH;
+static const char *sysfs_resctrl_path = SYSFS_RESCTRL_PATH;


 void virSysfsSetSystemPath(const char *path)
@@ -55,6 +58,20 @@ virSysfsGetSystemPath(void)
     return sysfs_system_path;
 }

+void virSysfsSetResctrlPath(const char *path)
+{
+    if (path)
+        sysfs_resctrl_path  = path;
+    else
+        sysfs_resctrl_path = SYSFS_RESCTRL_PATH;
+}
+
+const char *
+virSysfsGetResctrlPath(void)
+{
+    return sysfs_resctrl_path;
+}
+
 int
 virSysfsGetValueInt(const char *file,
                     int *value)
@@ -227,3 +244,129 @@ virSysfsGetNodeValueBitmap(unsigned int node,
     VIR_FREE(path);
     return ret;
 }
+
+int
+virSysfsGetResctrlString(const char* file,
+                         char **value)
+{
+    char *path = NULL;
+    int ret = -1;
+    if (virAsprintf(&path, "%s/%s", sysfs_resctrl_path, file) < 0)
+        return -1;
+
+    if (virFileReadAll(path, VIR_SYSFS_VALUE_MAXLEN, value) < 0)
+        goto cleanup;
+
+    ret = 0;
+
+ cleanup:
+    VIR_FREE(path);
+    return ret;
+}
+
+int
+virSysfsGetResctrlUint(const char *file,
+                       unsigned int *value)
+{
+    char *path = NULL;
+    int ret = -1;
+    if (virAsprintf(&path, "%s/%s", sysfs_resctrl_path, file) < 0)
+        return -1;
+
+    ret = virFileReadValueUint(path, value);
+
+    VIR_FREE(path);
+    return ret;
+}
+
+int
+virSysfsGetResctrlInfoString(const char* file,
+                             char **value)
+{
+    char *path = NULL;
+    int ret = -1;
+    if (virAsprintf(&path, "%s/info/%s", sysfs_resctrl_path, file) < 0)
+        return -1;
+
+    if (virFileReadAll(path, VIR_SYSFS_VALUE_MAXLEN, value) < 0)
+        goto cleanup;
+
+    ret = 0;
+
+ cleanup:
+    VIR_FREE(path);
+    return ret;
+}
+
+int
+virSysfsGetResctrlInfoUint(const char *file,
+                           unsigned int *value)
+{
+    char *path = NULL;
+    int ret;
+
+    if (virAsprintf(&path, "%s/info/%s", sysfs_resctrl_path, file) < 0)
+        return -1;
+
+    ret = virFileReadValueUint(path, value);
+
+    VIR_FREE(path);
+    return ret;
+}
+
+int
+virSysfsWriteResctrlString(const char *file,
+                           const char *content)
+{
+    char *path = NULL;
+    int ret = -1;
+    int writefd;
+
+    if (virAsprintf(&path, "%s/%s", sysfs_resctrl_path, file) < 0)
+        return -1;
+
+    /* We can not use virFileWriteStr because resctrl requires oflag should be as
+     O_WRONLY | O_APPEND */
+    if ((writefd = open(path, O_WRONLY | O_APPEND, S_IRUSR | S_IWUSR)) < 0)
+        goto cleanup;
+
+    if (safewrite(writefd, content, strlen(content)) < 0)
+        goto cleanup;
+
+    ret = 0;
+
+ cleanup:
+    VIR_FORCE_CLOSE(writefd);
+    VIR_FREE(path);
+    return ret;
+}
+
+int
+virSysfsCreateResCtrlDir(const char *file)
+{
+    char *path = NULL;
+    int ret = -1;
+
+    if (virAsprintf(&path, "%s/%s", sysfs_resctrl_path, file) < 0)
+        return -1;
+
+    ret = virDirCreate(path, 0755, 0, 0, 0);
+
+    VIR_FREE(path);
+    return ret;
+}
+
+int
+virSysfsRemoveResCtrlDir(const char *file)
+{
+    char *path = NULL;
+    int ret = -1;
+
+    if (virAsprintf(&path, "%s/%s", sysfs_resctrl_path, file) < 0)
+        return -1;
+
+    ret = virFileDeleteTree(path);
+
+    VIR_FREE(path);
+    return ret;
+}
diff --git a/src/util/virsysfs.h b/src/util/virsysfs.h
index cd871ff..60ad444 100644
--- a/src/util/virsysfs.h
+++ b/src/util/virsysfs.h
@@ -25,6 +25,7 @@
 # include "virbitmap.h"

 const char * virSysfsGetSystemPath(void);
+const char * virSysfsGetResctrlPath(void);

 int
 virSysfsGetValueInt(const char *file,
@@ -67,4 +68,29 @@ virSysfsGetNodeValueBitmap(unsigned int cpu,
                            const char *file,
                            virBitmapPtr *value);

+int
+virSysfsGetResctrlString(const char* file,
+                         char **value);
+
+int
+virSysfsGetResctrlUint(const char* file,
+                       unsigned int *value);
+
+int
+virSysfsGetResctrlInfoString(const char* file,
+                             char **value);
+
+int
+virSysfsGetResctrlInfoUint(const char *file,
+                           unsigned int *value);
+
+int
+virSysfsWriteResctrlString(const char *file,
+                           const char *content);
+
+int
+virSysfsCreateResCtrlDir(const char *file);
+
+int
+virSysfsRemoveResCtrlDir(const char *file);
 #endif /* __VIR_SYSFS_H__*/
diff --git a/src/util/virsysfspriv.h b/src/util/virsysfspriv.h
index ae9f54a..bf36082 100644
--- a/src/util/virsysfspriv.h
+++ b/src/util/virsysfspriv.h
@@ -24,5 +24,6 @@
 # include "virsysfs.h"

 void virSysfsSetSystemPath(const char *path);
+void virSysfsSetResctrlPath(const char *path);

 #endif /* __VIR_SYSFS_PRIV_H__*/
--
1.9.1




More information about the libvir-list mailing list