* limitations under the License.
*/
+#define _GNU_SOURCE
+#include <errno.h>
+#include <fcntl.h>
+#include <glib.h>
+#include <limits.h>
+#include <linux/limits.h>
+#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
-#include <stdint.h>
-#include <unistd.h>
-#include <sys/types.h>
#include <string.h>
-#include <glib.h>
-
+#include <sys/stat.h>
+#include <sys/types.h>
+#include <unistd.h>
#include <bundle_internal.h>
+#include <dlog.h>
+
#include "aul.h"
-#define ROOT_UID 0
-#define TMP_FILE "/tmp/.testpkg"
+#undef LOG_TAG
+#define LOG_TAG "DEBUG"
+
+#undef _E
+#define _E LOGE
+
+#undef _W
+#define _W LOGW
-static char **gargv;
-static int gargc;
-bundle *kb = NULL;
-static int debugFlag = 0;
+#undef _I
+#define _I LOGI
-static GMainLoop *mainloop = NULL;
+#undef _D
+#define _D LOGD
-static bundle *create_internal_bundle()
+#define auto_ptr(x) __attribute__ ((__cleanup__(x)))
+
+#define PAYLOAD_SIZE (1024 * 1024 * 1000)
+
+static unsigned int __convert_string(const char *str)
{
- bundle *kb;
+ while (*str < '0' || *str > '9')
+ str++;
- kb = bundle_create();
- bundle_add(kb, AUL_K_DEBUG, "1");
- return kb;
+ return atoi(str);
}
-int launch(int debug_option)
+static int __open_file(const char *file, FILE **fp)
{
- int pid;
-
- if (!debug_option)
- pid = aul_open_app(gargv[1]);
- else {
- kb = create_internal_bundle();
- if (kb == NULL) {
- printf("bundle creation fail\n");
- return -1;
- }
- pid = aul_launch_app(gargv[1], kb);
- }
- return pid;
+ int ret;
+
+ *fp = fopen(file, "r");
+ if (!*fp) {
+ ret = -errno;
+ _E("Failed to open %s. errno(%d)", file, errno);
+ return ret;
+ }
+
+ return 0;
}
-void print_usage(char *progname)
+static void __close_file(FILE **fp)
{
- printf("[usage] %s <appid> [-d]\n",
- progname);
+ if (!fp || !*fp)
+ return;
+
+ fclose(*fp);
}
-static int __launch_app_dead_handler(int pid, void *data)
+int __proc_get_mem_used_ratio(unsigned int *mem_used_ratio)
{
- int listen_pid = (intptr_t)data;
-
- if (listen_pid == pid)
- g_main_loop_quit(mainloop);
+ auto_ptr(__close_file) FILE *fp = NULL;
+ char buf[LINE_MAX];
+ char *str;
+ unsigned int mem_free = 0;
+ unsigned int mem_total = 0;
+ unsigned int mem_available = 0;
+ unsigned int cached = 0;
+ unsigned int used;
+ unsigned int used_ratio;
+ int ret;
+
+ if (!mem_used_ratio) {
+ _E("Invalid parameter");
+ return -EINVAL;
+ }
+
+ ret = __open_file("/proc/meminfo", &fp);
+ if (ret != 0)
+ return ret;
+
+ while (fgets(buf, sizeof(buf), fp) != NULL) {
+ if ((str = strstr(buf, "MemTotal:"))) {
+ str += strlen("MemTotal:");
+ mem_total = __convert_string(str);
+ } else if ((str = strstr(buf, "MemFree:"))) {
+ str += strlen("MemFree:");
+ mem_free = __convert_string(str);
+ } else if ((str = strstr(buf, "MemAvailable:"))) {
+ str += strlen("MemAvailable:");
+ mem_available = __convert_string(str);
+ } else if ((str = strstr(buf, "Cached:")) &&
+ !strstr(buf, "Swap")) {
+ str += strlen("Cached:");
+ cached = atoi(str);
+ break;
+ }
+ }
+
+ if (mem_total == 0) {
+ _E("Failed to get total memory size");
+ return -1;
+ }
+
+ if (mem_available == 0)
+ mem_available = mem_free + cached;
+
+ used = mem_total - mem_available;
+ used_ratio = used * 100 / mem_total;
+
+ *mem_used_ratio = used_ratio;
+
+ return 0;
+}
- return 0;
+int __proc_get_mem_pss(int pid, unsigned int *mem_pss)
+{
+ auto_ptr(__close_file) FILE *fp = NULL;
+ char path[PATH_MAX];
+ char buf[LINE_MAX];
+ unsigned int pss = 0;
+ unsigned int total_pss = 0;
+ int ret;
+
+ if (pid < 1 || !mem_pss) {
+ _E("Invalid parameter");
+ return -EINVAL;
+ }
+
+ snprintf(path, sizeof(path), "/proc/%d/smaps", pid);
+ ret = __open_file(path, &fp);
+ if (ret != 0)
+ return ret;
+
+ while (fgets(buf, sizeof(buf), fp) != NULL) {
+ if (sscanf(buf, "Pss: %d kB", &pss) == 1) {
+ total_pss += pss;
+ pss = 0;
+ }
+ }
+
+ *mem_pss = total_pss;
+
+ return 0;
}
-static gboolean run_func(void *data)
+static void __check_mem(void)
{
- int pid;
- const char *str;
-
- if ((pid = launch(debugFlag)) > 0)
- printf("... successfully launched\n");
- else
- printf("... launch failed\n");
-
- if (kb) {
- str = bundle_get_val(kb, "__LAUNCH_APP_MODE__");
-
- if (str && strcmp(str, "SYNC") == 0)
- aul_listen_app_dead_signal(__launch_app_dead_handler, (void *)(intptr_t)pid);
- else
- g_main_loop_quit(mainloop);
-
- bundle_free(kb);
- kb = NULL;
- } else {
- g_main_loop_quit(mainloop);
- }
+ unsigned int mem_pss = 0;
+ unsigned int mem_used_ratio = 0;
+ __proc_get_mem_used_ratio(&mem_used_ratio);
+ __proc_get_mem_pss(getpid(), &mem_pss);
- return TRUE;
+ printf("== MEM CHECK [START] ==\n");
+ printf(" - memory used ratio: %u %%\n", mem_used_ratio);
+ printf(" - memory pss: %u kB\n", mem_pss);
+ printf("== MEM CHECK [END] ==\n");
}
-
-int main(int argc, char **argv)
+static gboolean __timeout_cb(gpointer data)
{
- if ((argc < 2) || (argc > 3)) {
- print_usage(argv[0]);
- exit(EXIT_FAILURE);
+ char *ptr;
+
+ printf("== TIMED OUT [START] ==\n");
+ __check_mem();
+ ptr = malloc(sizeof(char) * PAYLOAD_SIZE);
+ if (!ptr) {
+ fprintf(stderr, "Out of memory\n");
+ printf("== TIMED OUT [END] ==\n");
+ return G_SOURCE_REMOVE;
}
+ printf("== AFTER ALLOCATION ==\n");
+ __check_mem();
+ printf("== TIMED OUT [END] ==\n");
- gargc = argc;
- gargv = argv;
-
- if (argc == 3) {
- if ((strcmp(argv[2], "-d") != 0) && (strcmp(argv[1], "-d") != 0)) {
- printf("additionnal argument should be -d to enable debugging\n");
- print_usage(argv[0]);
- exit(EXIT_FAILURE);
- }
- debugFlag = 1;
- }
- aul_launch_init(NULL, NULL);
+ return G_SOURCE_CONTINUE;
+}
- g_idle_add(run_func, NULL);
+int main(int argc, char **argv)
+{
+ GMainLoop *loop;
- mainloop = g_main_loop_new(NULL, FALSE);
- if (!mainloop) {
- printf("failed to create glib main loop\n");
- exit(EXIT_FAILURE);
- }
- g_main_loop_run(mainloop);
+ __check_mem();
+ loop = g_main_loop_new(NULL, FALSE);
+ g_timeout_add(500, __timeout_cb, loop);
+ g_main_loop_run(loop);
+ g_main_loop_unref(loop);
return 0;
}
-