Consolidate code for getting platform details into one place (#311)

Consolidate code for getting platform details into a single place
This commit is contained in:
Ryan Mast
2024-12-09 15:21:50 -08:00
committed by GitHub
parent a8bd7ac723
commit d70e3326da
5 changed files with 85 additions and 43 deletions
+2 -2
View File
@@ -59,7 +59,7 @@ C = $(wildcard *.c) $(wildcard *.cpp)
INCLUDES = -I/usr/local/include -I. -Iclipper2/include INCLUDES = -I/usr/local/include -I. -Iclipper2/include
LIBS = -L/usr/local/lib LIBS = -L/usr/local/lib
tippecanoe: geojson.o jsonpull/jsonpull.o tile.o pool.o mbtiles.o geometry.o projection.o memfile.o mvt.o serial.o main.o text.o dirtiles.o pmtiles_file.o plugin.o read_json.o write_json.o geobuf.o flatgeobuf.o evaluator.o geocsv.o csv.o geojson-loop.o json_logger.o visvalingam.o compression.o clip.o sort.o attribute.o thread.o shared_borders.o clipper2/src/clipper.engine.o tippecanoe: geojson.o jsonpull/jsonpull.o tile.o pool.o mbtiles.o geometry.o projection.o memfile.o mvt.o serial.o main.o platform.o text.o dirtiles.o pmtiles_file.o plugin.o read_json.o write_json.o geobuf.o flatgeobuf.o evaluator.o geocsv.o csv.o geojson-loop.o json_logger.o visvalingam.o compression.o clip.o sort.o attribute.o thread.o shared_borders.o clipper2/src/clipper.engine.o
$(CXX) $(PG) $(LIBS) $(FINAL_FLAGS) $(CXXFLAGS) -o $@ $^ $(LDFLAGS) -lm -lz -lsqlite3 -lpthread $(CXX) $(PG) $(LIBS) $(FINAL_FLAGS) $(CXXFLAGS) -o $@ $^ $(LDFLAGS) -lm -lz -lsqlite3 -lpthread
tippecanoe-enumerate: enumerate.o tippecanoe-enumerate: enumerate.o
@@ -68,7 +68,7 @@ tippecanoe-enumerate: enumerate.o
tippecanoe-decode: decode.o projection.o mvt.o write_json.o text.o jsonpull/jsonpull.o dirtiles.o pmtiles_file.o tippecanoe-decode: decode.o projection.o mvt.o write_json.o text.o jsonpull/jsonpull.o dirtiles.o pmtiles_file.o
$(CXX) $(PG) $(LIBS) $(FINAL_FLAGS) $(CXXFLAGS) -o $@ $^ $(LDFLAGS) -lm -lz -lsqlite3 $(CXX) $(PG) $(LIBS) $(FINAL_FLAGS) $(CXXFLAGS) -o $@ $^ $(LDFLAGS) -lm -lz -lsqlite3
tile-join: tile-join.o projection.o mbtiles.o mvt.o memfile.o dirtiles.o jsonpull/jsonpull.o text.o evaluator.o csv.o write_json.o pmtiles_file.o clip.o attribute.o thread.o read_json.o clipper2/src/clipper.engine.o tile-join: tile-join.o platform.o projection.o mbtiles.o mvt.o memfile.o dirtiles.o jsonpull/jsonpull.o text.o evaluator.o csv.o write_json.o pmtiles_file.o clip.o attribute.o thread.o read_json.o clipper2/src/clipper.engine.o
$(CXX) $(PG) $(LIBS) $(FINAL_FLAGS) $(CXXFLAGS) -o $@ $^ $(LDFLAGS) -lm -lz -lsqlite3 -lpthread $(CXX) $(PG) $(LIBS) $(FINAL_FLAGS) $(CXXFLAGS) -o $@ $^ $(LDFLAGS) -lm -lz -lsqlite3 -lpthread
tippecanoe-json-tool: jsontool.o jsonpull/jsonpull.o csv.o text.o geojson-loop.o tippecanoe-json-tool: jsontool.o jsonpull/jsonpull.o csv.o text.o geojson-loop.o
+9 -40
View File
@@ -21,7 +21,6 @@
#include <limits.h> #include <limits.h>
#include <sqlite3.h> #include <sqlite3.h>
#include <stdarg.h> #include <stdarg.h>
#include <sys/resource.h>
#include <pthread.h> #include <pthread.h>
#include <getopt.h> #include <getopt.h>
#include <signal.h> #include <signal.h>
@@ -67,6 +66,7 @@
#include "sort.hpp" #include "sort.hpp"
#include "attribute.hpp" #include "attribute.hpp"
#include "thread.hpp" #include "thread.hpp"
#include "platform.hpp"
static int low_detail = 12; static int low_detail = 12;
static int full_detail = -1; static int full_detail = -1;
@@ -187,7 +187,7 @@ void init_cpus() {
if (TIPPECANOE_MAX_THREADS != NULL) { if (TIPPECANOE_MAX_THREADS != NULL) {
CPUS = atoi_require(TIPPECANOE_MAX_THREADS, "TIPPECANOE_MAX_THREADS"); CPUS = atoi_require(TIPPECANOE_MAX_THREADS, "TIPPECANOE_MAX_THREADS");
} else { } else {
CPUS = sysconf(_SC_NPROCESSORS_ONLN); CPUS = get_num_avail_cpus();
} }
if (CPUS < 1) { if (CPUS < 1) {
@@ -202,13 +202,7 @@ void init_cpus() {
// Round down to a power of 2 // Round down to a power of 2
CPUS = 1 << (int) (log(CPUS) / log(2)); CPUS = 1 << (int) (log(CPUS) / log(2));
struct rlimit rl; MAX_FILES = get_max_open_files();
if (getrlimit(RLIMIT_NOFILE, &rl) != 0) {
perror("getrlimit");
exit(EXIT_PTHREAD);
} else {
MAX_FILES = rl.rlim_cur;
}
// Don't really want too many temporary files, because the file system // Don't really want too many temporary files, because the file system
// will start to bog down eventually // will start to bog down eventually
@@ -222,7 +216,7 @@ void init_cpus() {
long long fds[MAX_FILES]; long long fds[MAX_FILES];
long long i; long long i;
for (i = 0; i < MAX_FILES; i++) { for (i = 0; i < MAX_FILES; i++) {
fds[i] = open("/dev/null", O_RDONLY | O_CLOEXEC); fds[i] = open(get_null_device(), O_RDONLY | O_CLOEXEC);
if (fds[i] < 0) { if (fds[i] < 0) {
break; break;
} }
@@ -899,7 +893,7 @@ void radix1(int *geomfds_in, int *indexfds_in, int inputs, int prefix, int split
std::atomic<long long> indexpos(indexst.st_size); std::atomic<long long> indexpos(indexst.st_size);
int bytes = sizeof(struct index); int bytes = sizeof(struct index);
int page = sysconf(_SC_PAGESIZE); int page = get_page_size();
// Don't try to sort more than 2GB at once, // Don't try to sort more than 2GB at once,
// which used to crash Macs and may still // which used to crash Macs and may still
long long max_unit = 2LL * 1024 * 1024 * 1024; long long max_unit = 2LL * 1024 * 1024 * 1024;
@@ -1069,31 +1063,6 @@ void prep_drop_states(struct drop_state *ds, int maxzoom, int basezoom, double d
} }
} }
static size_t calc_memsize() {
size_t mem;
#ifdef __APPLE__
int64_t hw_memsize;
size_t len = sizeof(int64_t);
if (sysctlbyname("hw.memsize", &hw_memsize, &len, NULL, 0) < 0) {
perror("sysctl hw.memsize");
exit(EXIT_MEMORY);
}
mem = hw_memsize;
#else
long long pagesize = sysconf(_SC_PAGESIZE);
long long pages = sysconf(_SC_PHYS_PAGES);
if (pages < 0 || pagesize < 0) {
perror("sysconf _SC_PAGESIZE or _SC_PHYS_PAGES");
exit(EXIT_MEMORY);
}
mem = (long long) pages * pagesize;
#endif
return mem;
}
void radix(std::vector<struct reader> &readers, int nreaders, FILE *geomfile, FILE *indexfile, const char *tmpdir, std::atomic<long long> *geompos, int maxzoom, int basezoom, double droprate, double gamma) { void radix(std::vector<struct reader> &readers, int nreaders, FILE *geomfile, FILE *indexfile, const char *tmpdir, std::atomic<long long> *geompos, int maxzoom, int basezoom, double droprate, double gamma) {
// Run through the index and geometry for each reader, // Run through the index and geometry for each reader,
// splitting the contents out by index into as many // splitting the contents out by index into as many
@@ -1446,7 +1415,7 @@ std::pair<int, metadata> read_input(std::vector<source> &sources, char *fname, i
size_t dist_count = 0; size_t dist_count = 0;
double area_sum = 0; double area_sum = 0;
int files_open_before_reading = open("/dev/null", O_RDONLY | O_CLOEXEC); int files_open_before_reading = open(get_null_device(), O_RDONLY | O_CLOEXEC);
if (files_open_before_reading < 0) { if (files_open_before_reading < 0) {
perror("open /dev/null"); perror("open /dev/null");
exit(EXIT_OPEN); exit(EXIT_OPEN);
@@ -1887,7 +1856,7 @@ std::pair<int, metadata> read_input(std::vector<source> &sources, char *fname, i
} }
} }
int files_open_after_reading = open("/dev/null", O_RDONLY | O_CLOEXEC); int files_open_after_reading = open(get_null_device(), O_RDONLY | O_CLOEXEC);
if (files_open_after_reading < 0) { if (files_open_after_reading < 0) {
perror("open /dev/null"); perror("open /dev/null");
exit(EXIT_OPEN); exit(EXIT_OPEN);
@@ -3728,7 +3697,7 @@ int main(int argc, char **argv) {
signal(SIGPIPE, SIG_IGN); signal(SIGPIPE, SIG_IGN);
files_open_at_start = open("/dev/null", O_RDONLY | O_CLOEXEC); files_open_at_start = open(get_null_device(), O_RDONLY | O_CLOEXEC);
if (files_open_at_start < 0) { if (files_open_at_start < 0) {
perror("open /dev/null"); perror("open /dev/null");
exit(EXIT_OPEN); exit(EXIT_OPEN);
@@ -3878,7 +3847,7 @@ int main(int argc, char **argv) {
muntrace(); muntrace();
#endif #endif
i = open("/dev/null", O_RDONLY | O_CLOEXEC); i = open(get_null_device(), O_RDONLY | O_CLOEXEC);
// i < files_open_at_start is not an error, because reading from a pipe closes stdin // i < files_open_at_start is not an error, because reading from a pipe closes stdin
if (i > files_open_at_start) { if (i > files_open_at_start) {
fprintf(stderr, "Internal error: did not close all files: %d\n", i); fprintf(stderr, "Internal error: did not close all files: %d\n", i);
+56
View File
@@ -0,0 +1,56 @@
#include "platform.hpp"
#include <cstdlib>
#include <cstdio>
#include <unistd.h>
#include <sys/resource.h>
#if defined(__APPLE__) || defined(__FreeBSD__)
#include <sys/types.h>
#include <sys/sysctl.h>
#include <sys/param.h>
#include <sys/mount.h>
#endif
#include "errors.hpp"
long get_num_avail_cpus() {
return sysconf(_SC_NPROCESSORS_ONLN);
}
long get_page_size() {
return sysconf(_SC_PAGESIZE);
}
size_t calc_memsize() {
size_t mem;
#ifdef __APPLE__
int64_t hw_memsize;
size_t len = sizeof(int64_t);
if (sysctlbyname("hw.memsize", &hw_memsize, &len, NULL, 0) < 0) {
perror("sysctl hw.memsize");
exit(EXIT_MEMORY);
}
mem = hw_memsize;
#else
long long pagesize = sysconf(_SC_PAGESIZE);
long long pages = sysconf(_SC_PHYS_PAGES);
if (pages < 0 || pagesize < 0) {
perror("sysconf _SC_PAGESIZE or _SC_PHYS_PAGES");
exit(EXIT_MEMORY);
}
mem = (long long) pages * pagesize;
#endif
return mem;
}
size_t get_max_open_files() {
struct rlimit rl;
if (getrlimit(RLIMIT_NOFILE, &rl) != 0) {
perror("getrlimit");
exit(EXIT_PTHREAD);
}
return rl.rlim_cur;
}
+16
View File
@@ -0,0 +1,16 @@
#ifndef PLATFORM_HPP
#define PLATFORM_HPP
#include <cstddef>
long get_num_avail_cpus();
long get_page_size();
size_t calc_memsize();
size_t get_max_open_files();
constexpr const char *get_null_device() { return "/dev/null"; }
#endif // PLATFORM_HPP
+2 -1
View File
@@ -43,6 +43,7 @@
#include "errors.hpp" #include "errors.hpp"
#include "geometry.hpp" #include "geometry.hpp"
#include "thread.hpp" #include "thread.hpp"
#include "platform.hpp"
int pk = false; int pk = false;
int pC = false; int pC = false;
@@ -1199,7 +1200,7 @@ int main(int argc, char **argv) {
struct tileset_reader *readers = NULL; struct tileset_reader *readers = NULL;
CPUS = sysconf(_SC_NPROCESSORS_ONLN); CPUS = get_num_avail_cpus();
const char *TIPPECANOE_MAX_THREADS = getenv("TIPPECANOE_MAX_THREADS"); const char *TIPPECANOE_MAX_THREADS = getenv("TIPPECANOE_MAX_THREADS");
if (TIPPECANOE_MAX_THREADS != NULL) { if (TIPPECANOE_MAX_THREADS != NULL) {