com.questdb.misc.Os Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of questdb-core Show documentation
Show all versions of questdb-core Show documentation
QuestDB is High Performance Time Series Database
/*******************************************************************************
* ___ _ ____ ____
* / _ \ _ _ ___ ___| |_| _ \| __ )
* | | | | | | |/ _ \/ __| __| | | | _ \
* | |_| | |_| | __/\__ \ |_| |_| | |_) |
* \__\_\\__,_|\___||___/\__|____/|____/
*
* Copyright (C) 2014-2016 Appsicle
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License, version 3,
* as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see .
*
******************************************************************************/
package com.questdb.misc;
import com.questdb.ex.FatalError;
import com.questdb.ex.KerberosException;
import com.questdb.std.ObjList;
import com.questdb.std.str.CharSequenceZ;
import com.questdb.std.str.Path;
import com.sun.management.OperatingSystemMXBean;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.lang.management.ManagementFactory;
public final class Os {
public static final int WINDOWS = 3;
public static final int _32Bit = -2;
public static final int type;
public static final int OSX = 1;
public static final int LINUX = 2;
private static final int UNKNOWN = -1;
private static final OperatingSystemMXBean bean = (OperatingSystemMXBean) ManagementFactory.getOperatingSystemMXBean();
private Os() {
}
public static native int errno();
public static long forkExec(CharSequence args) {
ObjList paths = Chars.splitLpsz(args);
int n = paths.size();
try {
long argv = Unsafe.malloc((n + 1) * 8);
try {
long p = argv;
for (int i = 0; i < n; i++) {
Unsafe.getUnsafe().putLong(p, paths.getQuick(i).address());
p += 8;
}
Unsafe.getUnsafe().putLong(p, 0);
return forkExec(argv);
} finally {
Unsafe.free(argv, n + 1);
}
} finally {
for (int i = 0; i < n; i++) {
paths.getQuick(i).close();
}
}
}
public static int forkExecPid(long forkExecT) {
return Unsafe.getUnsafe().getInt(forkExecT + 8);
}
public static int forkExecReadFd(long forkExecT) {
return Unsafe.getUnsafe().getInt(forkExecT);
}
public static int forkExecWriteFd(long forkExecT) {
return Unsafe.getUnsafe().getInt(forkExecT + 4);
}
public static byte[] generateKerberosToken(CharSequence spn) throws KerberosException {
try (CharSequenceZ cs = new CharSequenceZ(spn)) {
final long struct = generateKrbToken(cs.address());
int status = Unsafe.getUnsafe().getInt(struct);
int bufLen = Unsafe.getUnsafe().getInt(struct + 4);
long ptoken = Unsafe.getUnsafe().getLong(struct + 8);
if (status != 0) {
freeKrbToken(struct);
throw new KerberosException(status);
}
byte[] token = new byte[bufLen];
for (int i = 0; i < bufLen; i++) {
token[i] = Unsafe.getUnsafe().getByte(ptoken + i);
}
freeKrbToken(struct);
return token;
}
}
public static native int getPid();
public static long getSystemMemory() {
return bean.getTotalPhysicalMemorySize();
}
@SuppressWarnings("EmptyMethod")
public static void init() {
}
private static native long generateKrbToken(long spn);
private static native void freeKrbToken(long struct);
private static native long forkExec(long argv);
private static void loadLib(String lib) {
InputStream is = Os.class.getResourceAsStream(lib);
if (is == null) {
throw new FatalError("Internal error: cannot find " + lib + ", broken package?");
}
try {
File tempLib = null;
try {
int dot = lib.indexOf('.');
tempLib = File.createTempFile(lib.substring(0, dot), lib.substring(dot));
// copy to tempLib
try (FileOutputStream out = new FileOutputStream(tempLib)) {
byte[] buf = new byte[4096];
while (true) {
int read = is.read(buf);
if (read == -1) {
break;
}
out.write(buf, 0, read);
}
} finally {
tempLib.deleteOnExit();
}
System.load(tempLib.getAbsolutePath());
} catch (IOException e) {
throw new FatalError("Internal error: cannot unpack " + tempLib, e);
}
} finally {
Misc.free(is);
}
}
static {
if ("64".equals(System.getProperty("sun.arch.data.model"))) {
String osName = System.getProperty("os.name");
if (osName.contains("Linux")) {
type = LINUX;
loadLib("/binaries/linux/libquestdb.so");
} else if (osName.contains("Mac")) {
type = OSX;
loadLib("/binaries/osx/libquestdb.dylib");
} else if (osName.contains("Windows")) {
type = WINDOWS;
loadLib("/binaries/windows/libquestdb.dll");
} else {
type = UNKNOWN;
}
} else {
type = _32Bit;
}
}
}