Merge pull request #98 from NativeScript/ios-uptime

Add function to obtain the time since the process started
This commit is contained in:
Panayot Cankov
2017-05-16 13:22:47 +03:00
committed by GitHub
6 changed files with 113 additions and 0 deletions

View File

@@ -0,0 +1,59 @@
package org.nativescript;
import android.os.SystemClock;
import android.util.Log;
import java.io.BufferedReader;
import java.io.FileReader;
/**
* Created by cankov on 15/05/2017.
*/
public class Process {
static long startTime = -1;
public static long getStartTime() {
if (startTime != -1) {
return startTime;
}
try {
int pid = android.os.Process.myPid();
final String path = "/proc/" + pid + "/stat";
final BufferedReader reader = new BufferedReader(new FileReader(path));
final String stat;
try {
stat = reader.readLine();
} finally {
reader.close();
}
final String field2End = ") ";
final String fieldSep = " ";
final int fieldStartTime = 20;
final int msInSec = 1000;
final String[] fields = stat.substring(stat.lastIndexOf(field2End)).split(fieldSep);
final long t = Long.parseLong(fields[fieldStartTime]);
int tckName;
try {
tckName = Class.forName("android.system.OsConstants").getField("_SC_CLK_TCK").getInt(null);
} catch (ClassNotFoundException e) {
tckName = Class.forName("libcore.io.OsConstants").getField("_SC_CLK_TCK").getInt(null);
}
final Object os = Class.forName("libcore.io.Libcore").getField("os").get(null);
final long tck = (Long) os.getClass().getMethod("sysconf", Integer.TYPE).invoke(os, tckName);
startTime = t * msInSec / tck;
} catch (Exception e) {
Log.v("JS", "Failed to get process start time. Using the current time as start time. Error: " + e);
startTime = SystemClock.elapsedRealtime();
}
return startTime;
}
public static long getUpTime() {
long startTime = getStartTime();
long currentTime = SystemClock.elapsedRealtime();
return currentTime - startTime;
}
}

View File

@@ -0,0 +1,17 @@
//
// TNSProcess.h
// TNSWidgets
//
// Created by Panayot Cankov on 15/05/2017.
// Copyright © 2017 Telerik A D. All rights reserved.
//
#ifndef TNSProcess_h
#define TNSProcess_h
/**
* Get the milliseconds since the process started.
*/
double __tns_uptime();
#endif /* TNSProcess_h */

View File

@@ -0,0 +1,28 @@
//
// TNSProcess.c
// TNSWidgets
//
// Created by Panayot Cankov on 15/05/2017.
// Copyright © 2017 Telerik A D. All rights reserved.
//
#include "TNSProcess.h"
#import <Foundation/Foundation.h>
#include <sys/sysctl.h>
#include <sys/types.h>
#include <sys/time.h>
double __tns_uptime() {
pid_t pid = [[NSProcessInfo processInfo] processIdentifier];
int mib[4] = { CTL_KERN, KERN_PROC, KERN_PROC_PID, pid };
struct kinfo_proc proc;
size_t size = sizeof(proc);
sysctl(mib, 4, &proc, &size, NULL, 0);
struct timeval current;
gettimeofday(&current, NULL);
return (double)(current.tv_sec - proc.kp_proc.p_starttime.tv_sec) * 1000.0 + (double)(current.tv_usec - proc.kp_proc.p_starttime.tv_usec) / 1000.0;
}

View File

@@ -9,6 +9,8 @@
/* Begin PBXBuildFile section */
8B7321CF1D097ECD00884AC6 /* TNSLabel.h in Headers */ = {isa = PBXBuildFile; fileRef = 8B7321CD1D097ECD00884AC6 /* TNSLabel.h */; settings = {ATTRIBUTES = (Public, ); }; };
8B7321D01D097ECD00884AC6 /* TNSLabel.m in Sources */ = {isa = PBXBuildFile; fileRef = 8B7321CE1D097ECD00884AC6 /* TNSLabel.m */; };
F915D3551EC9EF5E00071914 /* TNSProcess.m in Sources */ = {isa = PBXBuildFile; fileRef = F915D3531EC9EF5E00071914 /* TNSProcess.m */; };
F915D3561EC9EF5E00071914 /* TNSProcess.h in Headers */ = {isa = PBXBuildFile; fileRef = F915D3541EC9EF5E00071914 /* TNSProcess.h */; settings = {ATTRIBUTES = (Public, ); }; };
F98F5CB31CD0EFEA00978308 /* TNSWidgets.h in Headers */ = {isa = PBXBuildFile; fileRef = F98F5CB21CD0EFEA00978308 /* TNSWidgets.h */; settings = {ATTRIBUTES = (Public, ); }; };
F98F5CBA1CD0EFEA00978308 /* TNSWidgets.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = F98F5CAF1CD0EFEA00978308 /* TNSWidgets.framework */; };
F98F5CBF1CD0EFEA00978308 /* TNSWidgetsTests.m in Sources */ = {isa = PBXBuildFile; fileRef = F98F5CBE1CD0EFEA00978308 /* TNSWidgetsTests.m */; };
@@ -29,6 +31,8 @@
/* Begin PBXFileReference section */
8B7321CD1D097ECD00884AC6 /* TNSLabel.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = TNSLabel.h; sourceTree = "<group>"; };
8B7321CE1D097ECD00884AC6 /* TNSLabel.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = TNSLabel.m; sourceTree = "<group>"; };
F915D3531EC9EF5E00071914 /* TNSProcess.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = TNSProcess.m; path = ../TNSProcess.m; sourceTree = "<group>"; };
F915D3541EC9EF5E00071914 /* TNSProcess.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = TNSProcess.h; path = ../TNSProcess.h; sourceTree = "<group>"; };
F98F5CAF1CD0EFEA00978308 /* TNSWidgets.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = TNSWidgets.framework; sourceTree = BUILT_PRODUCTS_DIR; };
F98F5CB21CD0EFEA00978308 /* TNSWidgets.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = TNSWidgets.h; sourceTree = "<group>"; };
F98F5CB41CD0EFEA00978308 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
@@ -79,6 +83,8 @@
F98F5CB11CD0EFEA00978308 /* TNSWidgets */ = {
isa = PBXGroup;
children = (
F915D3531EC9EF5E00071914 /* TNSProcess.m */,
F915D3541EC9EF5E00071914 /* TNSProcess.h */,
F98F5CB21CD0EFEA00978308 /* TNSWidgets.h */,
8B7321CD1D097ECD00884AC6 /* TNSLabel.h */,
8B7321CE1D097ECD00884AC6 /* TNSLabel.m */,
@@ -105,6 +111,7 @@
isa = PBXHeadersBuildPhase;
buildActionMask = 2147483647;
files = (
F915D3561EC9EF5E00071914 /* TNSProcess.h in Headers */,
F98F5CB31CD0EFEA00978308 /* TNSWidgets.h in Headers */,
F98F5CCB1CD0F09E00978308 /* UIImage+TNSBlocks.h in Headers */,
8B7321CF1D097ECD00884AC6 /* TNSLabel.h in Headers */,
@@ -208,6 +215,7 @@
buildActionMask = 2147483647;
files = (
8B7321D01D097ECD00884AC6 /* TNSLabel.m in Sources */,
F915D3551EC9EF5E00071914 /* TNSProcess.m in Sources */,
F98F5CCC1CD0F09E00978308 /* UIImage+TNSBlocks.m in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;

View File

@@ -18,3 +18,4 @@ FOUNDATION_EXPORT const unsigned char TNSWidgetsVersionString[];
#import "UIImage+TNSBlocks.h"
#import "TNSLabel.h"
#import "TNSProcess.h"