-
Notifications
You must be signed in to change notification settings - Fork 294
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Refactor] Use ProcFs to fetch CPU info
Signed-off-by: Muntashir Al-Islam <muntashirakon@riseup.net>
- Loading branch information
1 parent
bfeab8c
commit 4656e49
Showing
5 changed files
with
272 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
85 changes: 85 additions & 0 deletions
85
app/src/main/java/io/github/muntashirakon/proc/ProcFdInfoList.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
// SPDX-License-Identifier: GPL-3.0-or-later | ||
|
||
package io.github.muntashirakon.proc; | ||
|
||
import androidx.annotation.NonNull; | ||
import androidx.annotation.Nullable; | ||
|
||
import java.util.Collection; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
import io.github.muntashirakon.AppManager.utils.FileUtils; | ||
import io.github.muntashirakon.io.Path; | ||
|
||
public class ProcFdInfoList { | ||
public static class ProcFdInfo { | ||
public final int id; | ||
public final long offset; | ||
public final int mode; | ||
public final int mountId; | ||
@NonNull | ||
public final Path realPath; | ||
@NonNull | ||
public final String[] extras; | ||
|
||
private ProcFdInfo(int id, @NonNull Path realPath, @NonNull String[] info) { | ||
this.id = id; | ||
this.realPath = realPath; | ||
this.extras = new String[info.length - 3]; // Ignore three mandatory fields | ||
int i = 0; | ||
long offset = -1; | ||
int mode = -1; | ||
int mountId = -1; | ||
for (String line : info) { | ||
if (line.startsWith("pos:")) { | ||
offset = Long.decode(line.substring(4).trim()); | ||
} else if (line.startsWith("flags:")) { | ||
mode = Integer.decode(line.substring(6).trim()); | ||
} else if (line.startsWith("mnt_id:")) { | ||
mountId = Integer.decode(line.substring(7).trim()); | ||
} else { | ||
extras[i++] = line; | ||
} | ||
} | ||
assert offset != -1; | ||
assert mode != -1; | ||
assert mountId != -1; | ||
|
||
this.offset = offset; | ||
this.mode = mode; | ||
this.mountId = mountId; | ||
} | ||
|
||
public String getModeString() { | ||
return FileUtils.translateModePosixToString(mode); | ||
} | ||
} | ||
|
||
private final Map<Integer, ProcFdInfo> mFdInfoMap; | ||
|
||
ProcFdInfoList(@NonNull Path[] fdFiles, @NonNull String[] fdInfoList) { | ||
assert fdFiles.length == fdInfoList.length; | ||
mFdInfoMap = new HashMap<>(fdFiles.length); | ||
for (int i = 0; i < fdFiles.length; ++i) { | ||
String fdInfo = fdInfoList[i]; | ||
if (fdInfo == null) { | ||
// FD no longer exists | ||
continue; | ||
} | ||
Path fdFile = fdFiles[i]; | ||
int fd = Integer.decode(fdFile.getName()); | ||
ProcFdInfo procFdInfo = new ProcFdInfo(fd, fdFile, fdInfo.split("\\n")); | ||
mFdInfoMap.put(fd, procFdInfo); | ||
} | ||
} | ||
|
||
public Collection<Integer> getFds() { | ||
return mFdInfoMap.keySet(); | ||
} | ||
|
||
@Nullable | ||
public ProcFdInfo getFdInfo(int fd) { | ||
return mFdInfoMap.get(fd); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
63 changes: 63 additions & 0 deletions
63
app/src/main/java/io/github/muntashirakon/proc/ProcMappedFiles.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
// SPDX-License-Identifier: GPL-3.0-or-later | ||
|
||
package io.github.muntashirakon.proc; | ||
|
||
import androidx.annotation.NonNull; | ||
import androidx.annotation.Nullable; | ||
|
||
import java.io.IOException; | ||
import java.util.Collection; | ||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.Objects; | ||
|
||
import io.github.muntashirakon.io.Path; | ||
import kotlin.collections.ArrayDeque; | ||
|
||
public class ProcMappedFiles { | ||
public static class MappedFile { | ||
public final Path memoryPath; | ||
public final String realPath; | ||
public final long vmStart; | ||
public final long vmEnd; | ||
|
||
private MappedFile(@NonNull Path memoryPath, @NonNull String realPath) { | ||
this.memoryPath = memoryPath; | ||
this.realPath = realPath; | ||
String[] vmAreaStruct = memoryPath.getName().split("-"); | ||
vmStart = Long.decode("0x" + vmAreaStruct[0]); | ||
vmEnd = Long.decode("0x" + vmAreaStruct[1]); | ||
} | ||
} | ||
|
||
private final Map<String, List<MappedFile>> mMappedFiles; | ||
|
||
ProcMappedFiles(@NonNull Path[] mappedFiles) { | ||
mMappedFiles = new HashMap<>(mappedFiles.length); | ||
for (Path file : mappedFiles) { | ||
try { | ||
String realPath = Objects.requireNonNull(file.getRealFilePath()); | ||
MappedFile mappedFile = new MappedFile(file, realPath); | ||
List<MappedFile> mappedFileList = mMappedFiles.get(realPath); | ||
if (mappedFileList == null) { | ||
mappedFileList = new ArrayDeque<>(); | ||
mMappedFiles.put(realPath, mappedFileList); | ||
} | ||
mappedFileList.add(mappedFile); | ||
} catch (IOException e) { | ||
e.printStackTrace(); | ||
} | ||
} | ||
} | ||
|
||
@NonNull | ||
public Collection<String> getRealFiles() { | ||
return mMappedFiles.keySet(); | ||
} | ||
|
||
@Nullable | ||
public List<MappedFile> getMappedFiles(@NonNull String realPath) { | ||
return mMappedFiles.get(realPath); | ||
} | ||
} |