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