2137e7c761086ec9c8ee94d04b09d01c17eeb3bf
[platform/framework/native/appfw.git] / src / system / FSys_RuntimeInfoImpl.cpp
1 //
2 // Open Service Platform
3 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
4 //
5 // Licensed under the Apache License, Version 2.0 (the License);
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
8 //
9 //     http://www.apache.org/licenses/LICENSE-2.0
10 //
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
16 //
17
18 /**
19  * @file                FSysRuntimeInfo.cpp
20  * @brief               This is the implementation file for _RuntimeInfoImpl class.
21  *
22  *
23  * This file contains the implementation of _RuntimeInfoImpl class.
24  */
25
26 #include <unique_ptr.h>
27 #include <pthread.h>
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <string.h>
31 #include <unistd.h>
32 #include <sys/vfs.h>
33 #include <app_storage.h>
34 #include <devman.h>
35 #include <runtime_info.h>
36 #include <vconf.h>
37
38 #include <FAppApp.h>
39 #include <FBaseRtEvent.h>
40 #include <FBaseRtIEventArg.h>
41 #include <FBaseRtIEventListener.h>
42 #include <FBaseRtThread.h>
43 #include <FBaseSysLog.h>
44 #include <FApp_AppInfo.h>
45 #include <FBase_StringConverter.h>
46 #include <FSys_EnvironmentImpl.h>
47 #include <FSys_RuntimeInfoImpl.h>
48
49 using namespace Tizen::App;
50 using namespace Tizen::Base;
51 using namespace Tizen::Base::Runtime;
52
53 using namespace std;
54
55 namespace Tizen { namespace System
56 {
57
58 static const wchar_t* _ALLOCATED_MEMORY = L"AllocatedMemory";
59 static const wchar_t* _MEMORY_ALLOCATED = L"http://tizen.org/runtime/memory.allocated";
60 static const wchar_t* _MEMORY_ALLOCATED_SELF = L"http://tizen.org/runtime/memory.allocated.self";
61
62 static const wchar_t* _AVAILABLE_EXTERNAL_STORAGE = L"AvailableExternalStorage";
63 static const wchar_t* _STORAGE_AVAILABLE_EXTERNAL = L"http://tizen.org/runtime/storage.available.external";
64
65 static const wchar_t* _AVAILABLE_INTERNAL_STORAGE = L"AvailableInternalStorage";
66 static const wchar_t* _STORAGE_AVAILABLE_INTERNAL = L"http://tizen.org/runtime/storage.available.internal";
67
68 static const wchar_t* _AVAILABLE_MEDIA_STORAGE = L"AvailableMediaStorage";
69 static const wchar_t* _STORAGE_AVAILABLE_INTERNAL_MEDIA = L"http://tizen.org/runtime/storage.available.internal.media";
70
71 static const wchar_t* _BATTERY_LEVEL = L"BatteryLevel";
72 static const wchar_t* _CHARGING = L"IsCharging";
73
74 static const wchar_t* _CPU_CORE_ALL_USAGE = L"http://tizen.org/runtime/cpu.core.all.usage";
75
76 static const wchar_t* _MAX_ALLOCATABLE_MEMORY = L"MaxAllocatableMemory";
77 static const wchar_t* _AVAILABLE_VIDEO_MEMORY = L"AvailableVideoMemory";
78 static const wchar_t* _ALLOCATED_VIDEO_MEMORY = L"AllocatedVideoMemory";
79 static const wchar_t* _AVAILABLE_MEMORY = L"AvailableMemory";
80 static const wchar_t* _MEMORY_AVAILABLE = L"http://tizen.org/runtime/memory.available";
81 static const wchar_t* _MEMORY_AVAILABLE_VIDEO = L"http://tizen.org/runtime/memory.available.video";
82 static const wchar_t* _MEMORY_ALLOCATED_VIDEO = L"http://tizen.org/runtime/memory.allocated.video";
83
84 static const wchar_t* _STORAGE_ALLOCATED_INTERNAL_APPLICATION = L"http://tizen.org/runtime/storage.allocated.internal.application";
85 static const wchar_t* _STORAGE_ALLOCATED_INTERNAL_AUDIO = L"http://tizen.org/runtime/storage.allocated.internal.audio";
86 static const wchar_t* _STORAGE_ALLOCATED_INTERNAL_VIDEO = L"http://tizen.org/runtime/storage.allocated.internal.video";
87 static const wchar_t* _STORAGE_ALLOCATED_INTERNAL_IMAGE = L"http://tizen.org/runtime/storage.allocated.internal.image";
88 static const wchar_t* _STORAGE_ALLOCATED_INTERNAL_DOWNLOAD = L"http://tizen.org/runtime/storage.allocated.internal.download";
89 static const wchar_t* _STORAGE_ALLOCATED_EXTERNAL_APPLICATION = L"http://tizen.org/runtime/storage.allocated.external.application";
90 static const wchar_t* _STORAGE_ALLOCATED_EXTERNAL_AUDIO = L"http://tizen.org/runtime/storage.allocated.external.audio";
91 static const wchar_t* _STORAGE_ALLOCATED_EXTERNAL_VIDEO = L"http://tizen.org/runtime/storage.allocated.external.video";
92 static const wchar_t* _STORAGE_ALLOCATED_EXTERNAL_IMAGE = L"http://tizen.org/runtime/storage.allocated.external.image";
93 static const wchar_t* _STORAGE_ALLOCATED_EXTERNAL_DOWNLOAD = L"http://tizen.org/runtime/storage.allocated.external.download";
94
95 static const int _RUNTIME_INFO_EVENT_TYPE_INT = 101;
96 static const int _RUNTIME_INFO_EVENT_TYPE_LONGLONG = 102;
97
98
99 static const char* const _PROC_SYSTEM_MEMORY_INFO = "/proc/meminfo";
100 static const char* const _PROC_PROCESS_MEMORY_INFO = "/proc/self/status";
101 static const char* const _PROC_STAT = "/proc/stat";
102
103 static const char* const _PROC_KEY_SYSTEM_MEMORY_TOTAL = "MemTotal";
104 static const char* const _PROC_KEY_SYSTEM_MEMORY_FREE = "MemFree";
105 static const char* const _PROC_KEY_PROCESS_MEMORY = "VmSize";
106
107 static const char* const _ROOT_PATH = "/";
108
109 static const int _MAX_BUFFER_LENGTH = 1024;
110
111 static const int _CPU_USAGE_EVALUATION_INTERVAL = 1000000; //1 sec
112
113 typedef struct {
114     char cpu[20];
115     unsigned long long user;
116     unsigned long long system;
117     unsigned long long nice;
118     unsigned long long idle;
119     unsigned long long wait;
120     unsigned long long hi;
121     unsigned long long si;
122     unsigned long long zero;
123 } procstat_t;
124
125 bool read_proc_stat(procstat_t *p)
126 {
127         FILE *fp = null;
128         int ret = 0;
129
130         fp = fopen(_PROC_STAT, "r");
131         if(fp == null)
132         {
133                 return false;
134         }
135
136         ret = fscanf(fp, "%s %llu %llu %llu %llu %llu %llu %llu %llu",
137         p->cpu,
138         &p->user,
139         &p->system,
140         &p->nice,
141         &p->idle,
142         &p->wait,
143         &p->hi,
144         &p->si,
145         &p->zero);
146
147         if(ret < 1)
148         {
149                 if(fp != null)
150                 {
151                         fclose(fp);
152                 }
153                 return false;
154         }
155
156         if(fp != null)
157         {
158                 fclose(fp);
159         }
160
161         return true;
162 }
163  
164 void diff_proc_stat(procstat_t *a, procstat_t *b, procstat_t *d)
165 {
166         d->user     = a->user   - b->user;
167         d->system   = a->system - b->system;
168         d->nice     = a->nice   - b->nice;
169         d->idle     = a->idle   - b->idle;
170         d->wait     = a->wait   - b->wait;
171         d->hi       = a->hi     - b->hi;
172         d->si       = a->si     - b->si;
173         d->zero     = a->zero   - b->zero;
174 }
175
176 class _RuntimeInfoEventArg : public IEventArg
177 {
178 public:
179         _RuntimeInfoEventArg()
180         : Type(0)
181         , intValue(0)
182         , longLongValue(0)
183         , errorCode(E_SUCCESS)
184         , DirectoryPath(null)
185         , pListener(null)
186         {
187         }
188
189         ~_RuntimeInfoEventArg()
190         {
191                 if(DirectoryPath != null)
192                 {
193                         delete [] DirectoryPath;
194                 }
195         }
196
197         int Type;
198         int intValue;
199         long long longLongValue;
200         result errorCode;
201         char* DirectoryPath;
202         IEventListener* pListener;
203 };
204
205 class _RuntimeInfoEvent : public Event
206 {
207 protected:
208         void FireImpl(IEventListener& listener, const IEventArg& arg)
209         {
210                 const _RuntimeInfoEventArg* runtimeArg = dynamic_cast<const _RuntimeInfoEventArg*> (&arg);
211
212                 if(runtimeArg == null)
213                 {
214                         SysLogException(NID_SYS, E_SYSTEM, "Argument is null.");
215                         return;
216                 }
217
218                 switch(runtimeArg->Type)
219                 {
220                 case _RUNTIME_INFO_EVENT_TYPE_INT:
221                 {
222                         IRuntimeInfoGetIntAsyncResultListener* pIntListener = dynamic_cast<IRuntimeInfoGetIntAsyncResultListener*>(&listener);
223
224                         if(pIntListener == runtimeArg->pListener)
225                         {                               
226                                 pIntListener->OnResultReceivedForGetValueAsync(runtimeArg->intValue, runtimeArg->errorCode);
227                                 RemoveListener(listener);
228                         }
229                 }
230                 break;
231                 case _RUNTIME_INFO_EVENT_TYPE_LONGLONG:
232                 {
233                         IRuntimeInfoGetLonglongAsyncResultListener* pLonglongListener = dynamic_cast<IRuntimeInfoGetLonglongAsyncResultListener*>(&listener);
234                         if(pLonglongListener == runtimeArg->pListener)
235                         {
236                                 pLonglongListener->OnResultReceivedForGetValueAsync(runtimeArg->longLongValue, runtimeArg->errorCode);
237                                 RemoveListener(listener);
238                         }
239                 }
240                 break;
241                 }
242         }
243 };
244
245 _RuntimeInfoEvent runtimeInfoEvent;
246
247 _RuntimeInfoImpl::_RuntimeInfoImpl(void)
248 {
249 }
250
251 _RuntimeInfoImpl::~_RuntimeInfoImpl(void)
252 {
253 }
254
255 result
256 _RuntimeInfoImpl::GetValue(const String& key, String& value)
257 {
258         return E_OBJ_NOT_FOUND;
259 }
260
261 result
262 _RuntimeInfoImpl::GetValue(const String& key, int& value)
263 {
264         int total_size = 0;
265         int free_size =0;
266         result r = E_OBJ_NOT_FOUND;
267
268         if (key == _BATTERY_LEVEL)
269         {
270                 return GetBatteryLevel(value);
271         }
272         else if (key == _ALLOCATED_MEMORY || key == _ALLOCATED_VIDEO_MEMORY)
273         {
274                 r = GetFromProc(_PROC_PROCESS_MEMORY_INFO, _PROC_KEY_PROCESS_MEMORY, free_size);
275                 SysTryReturnResult(NID_SYS, r == E_SUCCESS, E_SYSTEM, "It is failed to get memory size.");
276                 value = free_size * 1024;
277         }
278         else if (key == _AVAILABLE_MEMORY || key == _AVAILABLE_VIDEO_MEMORY || key == _MAX_ALLOCATABLE_MEMORY)
279         {
280                 r = GetFromProc(_PROC_SYSTEM_MEMORY_INFO, _PROC_KEY_SYSTEM_MEMORY_FREE, free_size);
281                 SysTryReturnResult(NID_SYS, r == E_SUCCESS, E_SYSTEM, "It is failed to get memory size.");
282                 value = free_size * 1024;
283         }
284         return r;
285 }
286
287 result
288 _RuntimeInfoImpl::GetValue(const String& key, long long& value)
289 {
290
291         long long total_size = 0;
292         long long free_size =0;
293         result r = E_OBJ_NOT_FOUND;
294
295         if (key == _MEMORY_AVAILABLE || key == _MEMORY_AVAILABLE_VIDEO)
296         {
297                 r = GetFromProcLonglong(_PROC_SYSTEM_MEMORY_INFO, _PROC_KEY_SYSTEM_MEMORY_FREE, free_size);
298                 SysTryReturnResult(NID_SYS, r == E_SUCCESS, E_SYSTEM, "It is failed to get memory size.");
299                 value = free_size * 1024;
300         }
301         else if (key == _MEMORY_ALLOCATED || key == _MEMORY_ALLOCATED_VIDEO)
302         {
303                 r = GetFromProcLonglong(_PROC_SYSTEM_MEMORY_INFO, _PROC_KEY_SYSTEM_MEMORY_TOTAL, total_size);
304                 SysTryReturnResult(NID_SYS, r == E_SUCCESS, E_SYSTEM, "It is failed to get memory size.");
305                 r = GetFromProcLonglong(_PROC_SYSTEM_MEMORY_INFO, _PROC_KEY_SYSTEM_MEMORY_FREE, free_size);
306                 SysTryReturnResult(NID_SYS, r == E_SUCCESS, E_SYSTEM, "It is failed to get memory size.");
307                 value = (total_size - free_size) * 1024;
308         }
309         else if (key == _MEMORY_ALLOCATED_SELF)
310         {   
311                 r = GetFromProcLonglong(_PROC_PROCESS_MEMORY_INFO, _PROC_KEY_PROCESS_MEMORY, free_size);
312                 SysTryReturnResult(NID_SYS, r == E_SUCCESS, E_SYSTEM, "It is failed to get memory size.");
313                 value = free_size * 1024;
314         } 
315         else if (key == _AVAILABLE_INTERNAL_STORAGE)
316         {
317                 return GetAvailableInternalStorage(value);
318         }
319         else if (key == _AVAILABLE_EXTERNAL_STORAGE)
320         {
321                 return GetAvailableExternalStorage(value);
322         }
323         else if (key == _AVAILABLE_MEDIA_STORAGE)
324         {
325                 return GetAvailableMediaStorage(value);
326         }
327         else if(key == _STORAGE_AVAILABLE_INTERNAL)
328         {
329                 return GetAvailableInternalStorage(value);
330         }
331         else if(key == _STORAGE_AVAILABLE_EXTERNAL)
332         {
333                 return GetAvailableExternalStorage(value);
334         }
335         else if(key == _STORAGE_AVAILABLE_INTERNAL_MEDIA)
336         {
337                 return GetAvailableMediaStorage(value);
338         }
339         else
340         {
341                 SysLogException(NID_SYS, r, "Key[%ls] is not valid.", key.GetPointer());
342         }
343         return r;
344 }
345
346 long long
347 _RuntimeInfoImpl::GetDirectorySize(const char* path)
348 {
349         if(path == null)
350         {
351                 return 0;
352         }
353
354         char command[512] = {0,};
355         char fileName[512] = {0,};
356         long long size = 0;
357         int ret = 0;
358
359         Tizen::App::App* pApp = Tizen::App::App::GetInstance();
360         if(pApp == null)
361         {
362                 return 0;
363         }
364
365         String appId(pApp->GetAppId());
366         unique_ptr <char> appIdPath(_StringConverter::CopyToCharArrayN(appId));
367
368         ret = sprintf(fileName, "/tmp/size_of_directory_%s.tmp", appIdPath.get());
369         if(ret < 1)
370         {
371                 SysLogException(NID_SYS, E_SYSTEM, "It is failed to write file path.");
372                 return 0;
373         }
374
375         ret = sprintf(command, "rm -rf %s", fileName);
376         if(ret < 1)
377         {
378                 SysLogException(NID_SYS, E_SYSTEM, "It is failed to write remove command.");
379                 return 0;
380         }
381
382         ret = system(command);
383
384         if(ret == -1)
385         {
386                 SysLogException(NID_SYS, E_SYSTEM, "It is failed to execute command[%s].", command);
387         }
388
389         ret = sprintf(command, "du -skb -P %s >> %s", path, fileName);
390         if(ret < 1)
391         {
392                 SysLogException(NID_SYS, E_SYSTEM, "It is failed to write du command.");
393                 return 0;
394         }
395
396         ret = system(command);
397         if(ret == -1)
398         {
399                 SysLogException(NID_SYS, E_SYSTEM, "It is failed to execute command[%s].", command);
400                 return 0;
401         }
402
403         FILE* pFile = null;
404         pFile = fopen(fileName, "r");
405         if(pFile == null)
406         {
407                 return 0;
408         }
409
410         ret = fscanf(pFile, "%lld", &size);
411         if(ret < 1)
412         {
413                 if(pFile != null)
414                 {
415                         fclose(pFile);
416                 }
417
418                 return 0;
419         }
420
421         if(pFile != null)
422         {
423                 fclose(pFile);
424         }
425
426         ret = sprintf(command, "rm -rf %s", fileName);
427         if(ret < 1)
428         {
429                 SysLogException(NID_SYS, E_SYSTEM, "It is failed to write remove command.");
430                 return 0;
431         }
432
433         ret = system(command);
434         if(ret == -1)
435         {
436                 SysLogException(NID_SYS, E_SYSTEM, "It is failed to execute command[%s].", command);
437         }
438
439
440         return size;
441 }
442
443 result
444 _RuntimeInfoImpl::GetValue(const String& key, double& value)
445 {
446         return E_OBJ_NOT_FOUND;
447 }
448
449 result
450 _RuntimeInfoImpl::GetValue(const String& key, bool& value)
451 {
452         if (key == _CHARGING)
453         {
454                 return IsChargingMode(value);
455         }
456         else
457         {
458                 return E_OBJ_NOT_FOUND;
459         }
460 }
461
462 result
463 _RuntimeInfoImpl::GetValue(const String& key, UuId& value)
464 {
465         return E_OBJ_NOT_FOUND;
466 }
467
468 result
469 _RuntimeInfoImpl::GetBatteryLevel(int& value)
470 {
471         int batteryValue = -1;
472         batteryValue = device_get_battery_pct();
473         SysTryReturnResult(NID_SYS, !(batteryValue < 0 || value > 100), E_SYSTEM, "[E_SYSTEM] device_get_battery_pct return %d", batteryValue);
474         value = batteryValue;
475
476         return E_SUCCESS;
477 }
478
479 result
480 _RuntimeInfoImpl::IsChargingMode(bool& value)
481 {
482         bool chargeState = false;
483         int ret = 0;
484         ret = runtime_info_get_value_bool(RUNTIME_INFO_KEY_BATTERY_IS_CHARGING, &chargeState);
485         SysTryReturnResult(NID_SYS, ret == 0, E_SYSTEM, "[E_SYSTEM] runtime_info_get_value_bool returns %d.", ret);
486         value = chargeState;
487         return E_SUCCESS;
488
489 }
490
491 result
492 _RuntimeInfoImpl::GetAvailableInternalStorage(long long& value)
493 {
494         long long total = 0;
495         result r = E_SUCCESS;
496
497         r = GetCapacity(_ROOT_PATH, total, value);
498         SysTryReturn(NID_SYS, !IsFailed(r), E_SYSTEM, r, "[E_SYSTEM] system error has occurred. ");
499
500         return E_SUCCESS;
501 }
502 result
503 _RuntimeInfoImpl::GetAvailableExternalStorage(long long& value)
504 {
505         int sdCardStatus = 0;
506         int ret = -1;
507         long long total = 0;
508
509         ret = vconf_get_int(VCONFKEY_SYSMAN_MMC_STATUS, &sdCardStatus);
510         SysTryReturn(NID_SYS, !(ret < 0), E_SYSTEM, E_SYSTEM, "[E_SYSTEM] vconf_get_int VCONFKEY_SYSMAN_MMC_STATUS failed");
511
512         if(sdCardStatus == 0)
513         {
514                 value = 0;
515                 return E_SUCCESS;
516         }
517         char* directoryPath = _StringConverter::CopyToCharArrayN(_EnvironmentImpl::GetExternalStoragePath());
518         SysTryReturnResult(NID_SYS, directoryPath != null, E_SYSTEM, "It is failed to get external directory path.");
519         result r = GetCapacity(directoryPath, total, value);
520         delete [] directoryPath;
521         return r ;
522 }
523
524 result
525 _RuntimeInfoImpl::GetAvailableMediaStorage(long long& value)
526 {
527         long long total = 0;
528
529         char* directoryPath = _StringConverter::CopyToCharArrayN(_EnvironmentImpl::GetMediaPath());
530         SysTryReturnResult(NID_SYS, directoryPath != null, E_SYSTEM, "It is failed to get media directory path.");
531         result r = GetCapacity(directoryPath, total, value);
532         delete [] directoryPath;
533         return r;
534 }
535
536 result
537 _RuntimeInfoImpl::GetCapacity(const char* path, long long& totalSize, long long& availableSize)
538 {
539         struct statfs fs;
540         if (statfs(path, &fs) < 0)
541         {
542                 SysLogException(NID_SYS, E_SYSTEM, "[E_SYSTEM] statfs() failed");
543                 return E_SYSTEM;
544         }
545
546         totalSize = static_cast< long long >(fs.f_bsize) * static_cast< long long >(fs.f_blocks);
547         availableSize = static_cast< long long >(fs.f_bsize) * static_cast< long long >(fs.f_bavail);
548
549         return E_SUCCESS;
550 }
551
552 result
553 _RuntimeInfoImpl::GetFromProcLonglong(const char* path, const char* key, long long& value)
554 {
555         FILE* pStream = null;
556         char line[_MAX_BUFFER_LENGTH] = {0, };
557         char fieldName[_MAX_BUFFER_LENGTH] = {0, };
558         long long keyValue = 0;
559         pStream = fopen(path, "r");
560         SysTryReturnResult(NID_SYS, pStream != null, E_SYSTEM, "%s open is failed.", path);
561
562         while (fgets(line, sizeof(line), pStream))
563         {
564                 if (sscanf(line, "%s %lld %*s", fieldName, &keyValue) == 2)
565                 {
566                         if (strncmp(fieldName, key, static_cast< int >(strlen(key))) == 0)
567                         {
568                                 value = keyValue;
569                                 fclose(pStream);
570                                 return E_SUCCESS;
571                         }
572                 }
573         }
574         fclose(pStream);
575
576         SysLogException(NID_SYS, E_OBJ_NOT_FOUND, "it can't find %s field", key);
577         return E_OBJ_NOT_FOUND;
578 }
579
580 result
581 _RuntimeInfoImpl::GetFromProc(const char* path, const char* key, int& value)
582 {
583         FILE* pStream = null;
584         char line[_MAX_BUFFER_LENGTH] = {0, };
585         char fieldName[_MAX_BUFFER_LENGTH] = {0, };
586         int keyValue = 0;
587         pStream = fopen(path, "r");
588         SysTryReturnResult(NID_SYS, pStream != null, E_SYSTEM, "%s open is failed.", path);
589
590         while (fgets(line, sizeof(line), pStream))
591         {
592                 if (sscanf(line, "%s %d %*s", fieldName, &keyValue) == 2)
593                 {
594                         if (strncmp(fieldName, key, static_cast< int >(strlen(key))) == 0)
595                         {
596                                 value = keyValue;
597                                 fclose(pStream);
598                                 return E_SUCCESS;
599                         }
600                 }
601         }
602         fclose(pStream);
603
604         SysLogException(NID_SYS, E_OBJ_NOT_FOUND, "it can't find %s field", key);
605         return E_OBJ_NOT_FOUND;
606 }
607
608 void*
609 _RuntimeInfoImpl::GetDirectorySizeAsync(void* data)
610 {       
611         if(data == null)
612         {               
613                 return null;
614         }
615         _RuntimeInfoEventArg* pEventArg = (_RuntimeInfoEventArg*)data;
616         long long size = _RuntimeInfoImpl::GetDirectorySize(pEventArg->DirectoryPath);
617
618         if(pEventArg == null)
619         {
620                 SysLogException(NID_SYS, E_OUT_OF_MEMORY, "It is failed to create instance of _RuntimeInfoEventArg");
621                 return null;
622         }
623         pEventArg->Type = _RUNTIME_INFO_EVENT_TYPE_LONGLONG;
624         pEventArg->longLongValue = size;
625         SysLog(NID_SYS, "Fire event %x %s %lld", pEventArg, pEventArg->DirectoryPath, size);
626         runtimeInfoEvent.Fire(*pEventArg);
627         
628         return null;
629 }
630
631 void*
632 _RuntimeInfoImpl::GetCpuUsageAsync(void* data)
633 {
634         result r = E_SUCCESS;
635
636         int value = 0;
637         procstat_t first, second, diffrence;
638         unsigned long long total, usage;
639
640         if(read_proc_stat(&first) != false)
641         {
642                 usleep(_CPU_USAGE_EVALUATION_INTERVAL);
643                 if(read_proc_stat(&second) != false)
644                 {
645                         diff_proc_stat(&second, &first, &diffrence);
646                         total = diffrence.user + diffrence.system 
647                                 + diffrence.nice + diffrence.idle 
648                                 + diffrence.wait + diffrence.hi 
649                                 + diffrence.si;
650                         usage = 10000 - (diffrence.idle * 10000 / total);
651                         value = usage/100;
652                         r = E_SUCCESS;
653                 }
654                 else
655                 {
656                         r = E_SYSTEM;
657                 }
658         }
659         else
660         {
661                 r = E_SYSTEM;
662         }
663
664         _RuntimeInfoEventArg* pEventArg = (_RuntimeInfoEventArg*) data;
665         if(pEventArg == null)
666         {
667                 SysLogException(NID_SYS, E_OUT_OF_MEMORY, "It is failed to create instance of _RuntimeInfoEventArg");
668                 return null;
669         }
670         pEventArg->errorCode = r;
671         pEventArg->Type = _RUNTIME_INFO_EVENT_TYPE_INT;
672         pEventArg->intValue = value;
673
674         runtimeInfoEvent.Fire(*pEventArg);
675
676         return null;
677 }
678
679 result
680 _RuntimeInfoImpl::GetValueAsync(const String& key, IRuntimeInfoGetIntAsyncResultListener* listener)
681 {
682         result r = E_SUCCESS;
683
684         SysTryReturnResult(NID_SYS, listener != null, E_INVALID_ARG, "listener is null");
685
686         if (key == _CPU_CORE_ALL_USAGE)
687         {
688                 if(listener != null)
689                 {
690                         runtimeInfoEvent.AddListener(*listener);
691                 }
692
693                 int thr_id = 0;
694                 pthread_t p_thread;
695                 _RuntimeInfoEventArg* pEventArg = new (std::nothrow) _RuntimeInfoEventArg();
696                 pEventArg->pListener = listener;
697                 thr_id = pthread_create(&p_thread, null, GetCpuUsageAsync, pEventArg);
698                 pthread_detach(p_thread);
699         }
700         else
701         {
702                 SysLogException(NID_SYS, E_INVALID_ARG, "Required key[%ls] is not valid", key.GetPointer());
703                 r = E_INVALID_ARG;
704         }
705         return r;
706 }
707
708 result
709 _RuntimeInfoImpl::GetValueAsync(const String& key, IRuntimeInfoGetLonglongAsyncResultListener* listener)
710 {
711         int thr_id = 0;
712         pthread_t p_thread;
713         result r = E_SUCCESS;
714         char* directoryPath = null;
715
716         SysTryReturnResult(NID_SYS, listener != null, E_INVALID_ARG, "listener is null");
717
718         if(key == _STORAGE_ALLOCATED_INTERNAL_APPLICATION)
719         {
720                 directoryPath = _StringConverter::CopyToCharArrayN(_EnvironmentImpl::GetPredefinedPath(PREDEFINED_DIRECTORY_APPLICATIONS));
721         }       
722         else if (key == _STORAGE_ALLOCATED_INTERNAL_AUDIO)
723         {
724                 directoryPath = _StringConverter::CopyToCharArrayN(_EnvironmentImpl::GetPredefinedPath(PREDEFINED_DIRECTORY_SOUNDS));
725         }
726         else if (key == _STORAGE_ALLOCATED_INTERNAL_VIDEO)
727         {
728                 directoryPath = _StringConverter::CopyToCharArrayN(_EnvironmentImpl::GetPredefinedPath(PREDEFINED_DIRECTORY_VIDEOS));
729         }
730         else if (key == _STORAGE_ALLOCATED_INTERNAL_IMAGE)
731         {
732                 directoryPath = _StringConverter::CopyToCharArrayN(_EnvironmentImpl::GetPredefinedPath(PREDEFINED_DIRECTORY_IMAGES));
733         }
734         else if (key == _STORAGE_ALLOCATED_INTERNAL_DOWNLOAD)
735         {
736                 directoryPath = _StringConverter::CopyToCharArrayN(_EnvironmentImpl::GetPredefinedPath(PREDEFINED_DIRECTORY_DOWNLOADS));
737         }
738         else if (key == _STORAGE_ALLOCATED_EXTERNAL_APPLICATION)
739         {
740                 directoryPath = _StringConverter::CopyToCharArrayN(_EnvironmentImpl::GetPredefinedPath(PREDEFINED_DIRECTORY_EXTERNAL_APPLICATIONS));
741         }
742         else if (key == _STORAGE_ALLOCATED_EXTERNAL_AUDIO)
743         {
744                 directoryPath = _StringConverter::CopyToCharArrayN(_EnvironmentImpl::GetPredefinedPath(PREDEFINED_DIRECTORY_EXTERNAL_SOUNDS));
745         }
746         else if (key == _STORAGE_ALLOCATED_EXTERNAL_VIDEO)
747         {
748                 directoryPath = _StringConverter::CopyToCharArrayN(_EnvironmentImpl::GetPredefinedPath(PREDEFINED_DIRECTORY_EXTERNAL_VIDEOS));
749         }
750         else if (key == _STORAGE_ALLOCATED_EXTERNAL_IMAGE)
751         {
752                 directoryPath = _StringConverter::CopyToCharArrayN(_EnvironmentImpl::GetPredefinedPath(PREDEFINED_DIRECTORY_EXTERNAL_IMAGES));
753         }
754
755         else if (key == _STORAGE_ALLOCATED_EXTERNAL_DOWNLOAD)
756         {
757                 directoryPath = _StringConverter::CopyToCharArrayN(_EnvironmentImpl::GetPredefinedPath(PREDEFINED_DIRECTORY_EXTERNAL_DOWNLOADS));
758         }
759         else
760         {
761                 SysLogException(NID_SYS, E_INVALID_ARG, "Required key[%ls] is not valid", key.GetPointer());
762                 r = E_INVALID_ARG;
763         }
764
765         SysLog(NID_SYS, "%s", GetErrorMessage(r));
766         SysLog(NID_SYS, "%s", directoryPath);
767         if(r == E_SUCCESS)
768         {
769                 if(listener != null)
770                 {
771                         SysLog(NID_SYS, "listener instance is %x.", listener);
772                         runtimeInfoEvent.AddListener(*listener);
773                 }
774
775                 _RuntimeInfoEventArg* pEventArg = new (std::nothrow) _RuntimeInfoEventArg();
776                 pEventArg->pListener = listener;
777                 pEventArg->DirectoryPath = directoryPath;
778
779                 thr_id = pthread_create(&p_thread, null, GetDirectorySizeAsync, pEventArg);
780                 SysLog(NID_SYS, "Thread id is %d.", thr_id);
781                 pthread_detach(p_thread);
782         }
783
784         return r;
785 }
786
787 _RuntimeInfoImpl*
788 _RuntimeInfoImpl::GetInstance(RuntimeInfo& runtimeinfo)
789 {
790         return runtimeinfo.__pRuntimeInfoImpl;
791 }
792
793 const _RuntimeInfoImpl*
794 _RuntimeInfoImpl::GetInstance(const RuntimeInfo& runtimeinfo)
795 {
796         return runtimeinfo.__pRuntimeInfoImpl;
797 }
798
799 } } //Tizen::System
800