* limitations under the License.
*/
+#include <errno.h>
#include <gio/gio.h>
#include <glib.h>
#include <stdio.h>
#include "proc-usage-application.h"
#include "proc-usage-process.h"
-GSList *proc_usage_application_get_list(void)
+/**
+ * @param[out] applist The appid list
+ * @return The number of apps if success, otherwise a negative error value
+ */
+int proc_usage_application_get_list(GSList **applist)
{
int ret;
+ int count = 0;
pid_t pid, pid_max;
DIR *dp;
struct dirent *de;
char label[64];
- GSList *applist = NULL;
struct proc_usage_application *pua;
+ if (!applist) {
+ _E("Invalid parameter");
+ return -EINVAL;
+ }
+
+ *applist = NULL;
+
ret = procfs_get_pid_max(&pid_max);
if (ret < 0) {
_E("Failed to get the maximum value of PID");
- return NULL;
+ return ret;
}
dp = opendir("/proc");
if (!dp) {
_E("Failed to open /proc : %m");
- return NULL;
+ return -errno;
}
while ((de = readdir(dp))) {
pua = g_new(struct proc_usage_application, 1);
if (!pua) {
_E("Not enough memory");
- if (applist)
- g_slist_free_full(applist, g_free);
+ if (*applist)
+ g_slist_free_full(*applist, g_free);
closedir(dp);
- return NULL;
+ return -ENOMEM;
}
strncpy(pua->appid, label + 11, NAME_MAX);
pua->pid = pid;
- applist = g_slist_prepend(applist, pua);
+ *applist = g_slist_prepend(*applist, pua);
+ ++count;
}
closedir(dp);
- return applist;
+ return count;
}
+/**
+ * @return NULL if error occurs. Otherwise, (appid, memory usage) list
+ * @notice If there is no launched app, return empty list.
+ * Empty list is different with NULL
+ */
GVariant *proc_usage_application_get_all_memory_usages(void)
{
int ret;
struct proc_usage_application *app = NULL;
struct procfs_pid_statm pps;
- g_variant_builder_init(&builder, G_VARIANT_TYPE("a(su)"));
-
- applist = proc_usage_application_get_list();
- if (!applist) {
- _E("Failed to get applist");
+ ret = proc_usage_application_get_list(&applist);
+ if (ret < 0) {
+ _E("Failed to get applist (%d)", ret);
return NULL;
}
+ g_variant_builder_init(&builder, G_VARIANT_TYPE("a(su)"));
+
G_SLIST_FOREACH_WITH_REMOVE(applist, app) {
g_assert(app);
appusage = g_variant_new("(su)", app->appid,
PAGE_TO_KBYTE(pps.resident - pps.shared));
g_assert(appusage);
-
g_variant_builder_add_value(&builder, appusage);
}
return g_variant_new("(a(su))", &builder);
}
+/**
+ * @return NULL if error occurs. Otherwise, (appid, CPU usage) list
+ * @notice If there is no launched app, return empty list.
+ * Empty list is different with NULL
+ */
GVariant *proc_usage_application_get_all_cpu_usages(void)
{
int ret;
struct proc_usage_application *app = NULL;
struct procfs_pid_stat pps;
- g_variant_builder_init(&builder, G_VARIANT_TYPE("a(su)"));
-
- applist = proc_usage_application_get_list();
- if (!applist) {
- _E("Failed to get applist");
- return NULL;
- }
-
hz = sysconf(_SC_CLK_TCK);
if (hz <= 0) {
_E("Failed to get Hertz : %m");
return NULL;
}
+ ret = proc_usage_application_get_list(&applist);
+ if (ret < 0) {
+ _E("Failed to get applist (%d)", ret);
+ return NULL;
+ }
+
+ g_variant_builder_init(&builder, G_VARIANT_TYPE("a(su)"));
+
G_SLIST_FOREACH_WITH_REMOVE(applist, app) {
g_assert(app);