From 7bd1550195a77f0dd7f3f67378eab6d26ed4ead1 Mon Sep 17 00:00:00 2001 From: Drazen Urch Date: Mon, 27 Mar 2023 16:34:10 +0200 Subject: [PATCH] libcpucycles (#3219) * Checkpoint * cpu cycle ffi * Rename * mixnode feature * Bundle libcpucycles --- .gitignore | 1 + Cargo.lock | 9 + cpu-cycles/Cargo.toml | 12 + cpu-cycles/build.rs | 65 +++ cpu-cycles/libcpucycles/Makefile | 8 + cpu-cycles/libcpucycles/autogen/html | 69 +++ cpu-cycles/libcpucycles/autogen/html-files | 7 + cpu-cycles/libcpucycles/autogen/html-style | 32 ++ cpu-cycles/libcpucycles/autogen/html-title | 1 + cpu-cycles/libcpucycles/autogen/man | 3 + .../libcpucycles/command/cpucycles-info.c | 93 ++++ cpu-cycles/libcpucycles/compilers/default | 2 + cpu-cycles/libcpucycles/configure | 309 ++++++++++++ cpu-cycles/libcpucycles/cpucycles/amd64-pmc.c | 53 ++ cpu-cycles/libcpucycles/cpucycles/amd64-tsc.c | 22 + .../libcpucycles/cpucycles/amd64-tscasm.c | 20 + .../libcpucycles/cpucycles/arm32-cortex.c | 27 ++ cpu-cycles/libcpucycles/cpucycles/arm64-pmc.c | 19 + cpu-cycles/libcpucycles/cpucycles/arm64-vct.c | 19 + cpu-cycles/libcpucycles/cpucycles/cpucycles.h | 25 + .../cpucycles/cpucycles_internal.h | 20 + .../cpucycles/default-gettimeofday.c | 15 + .../libcpucycles/cpucycles/default-mach.c | 17 + .../cpucycles/default-monotonic.c | 23 + .../cpucycles/default-perfevent.c | 101 ++++ .../libcpucycles/cpucycles/default-zero.c | 15 + cpu-cycles/libcpucycles/cpucycles/mips64-cc.c | 33 ++ cpu-cycles/libcpucycles/cpucycles/options | 19 + .../libcpucycles/cpucycles/ppc32-mftb.c | 30 ++ .../libcpucycles/cpucycles/ppc64-mftb.c | 30 ++ .../libcpucycles/cpucycles/riscv32-rdcycle.c | 39 ++ .../libcpucycles/cpucycles/riscv64-rdcycle.c | 29 ++ .../libcpucycles/cpucycles/s390x-stckf.c | 20 + .../libcpucycles/cpucycles/sparc64-rdtick.c | 24 + cpu-cycles/libcpucycles/cpucycles/wrapper.c | 420 ++++++++++++++++ cpu-cycles/libcpucycles/cpucycles/x86-tsc.c | 22 + .../libcpucycles/cpucycles/x86-tscasm.c | 22 + cpu-cycles/libcpucycles/doc/api.md | 47 ++ cpu-cycles/libcpucycles/doc/counters.md | 447 +++++++++++++++++ cpu-cycles/libcpucycles/doc/download.md | 30 ++ cpu-cycles/libcpucycles/doc/html/api.html | 91 ++++ .../libcpucycles/doc/html/counters.html | 456 ++++++++++++++++++ .../libcpucycles/doc/html/download.html | 75 +++ cpu-cycles/libcpucycles/doc/html/index.html | 88 ++++ cpu-cycles/libcpucycles/doc/html/install.html | 101 ++++ .../libcpucycles/doc/html/security.html | 122 +++++ .../libcpucycles/doc/html/selection.html | 158 ++++++ cpu-cycles/libcpucycles/doc/install.md | 56 +++ cpu-cycles/libcpucycles/doc/man/cpucycles.3 | 57 +++ cpu-cycles/libcpucycles/doc/readme.md | 36 ++ cpu-cycles/libcpucycles/doc/security.md | 76 +++ cpu-cycles/libcpucycles/doc/selection.md | 104 ++++ cpu-cycles/libcpucycles/scripts-build/install | 27 ++ .../libcpucycles/scripts-build/staticlib | 6 + cpu-cycles/libcpucycles/version | 1 + cpu-cycles/src/bindings.rs | 9 + cpu-cycles/src/lib.rs | 82 ++++ mixnode/Cargo.toml | 32 +- mixnode/src/main.rs | 5 + 59 files changed, 3768 insertions(+), 13 deletions(-) create mode 100644 cpu-cycles/Cargo.toml create mode 100644 cpu-cycles/build.rs create mode 100644 cpu-cycles/libcpucycles/Makefile create mode 100755 cpu-cycles/libcpucycles/autogen/html create mode 100644 cpu-cycles/libcpucycles/autogen/html-files create mode 100644 cpu-cycles/libcpucycles/autogen/html-style create mode 100644 cpu-cycles/libcpucycles/autogen/html-title create mode 100755 cpu-cycles/libcpucycles/autogen/man create mode 100644 cpu-cycles/libcpucycles/command/cpucycles-info.c create mode 100644 cpu-cycles/libcpucycles/compilers/default create mode 100755 cpu-cycles/libcpucycles/configure create mode 100644 cpu-cycles/libcpucycles/cpucycles/amd64-pmc.c create mode 100644 cpu-cycles/libcpucycles/cpucycles/amd64-tsc.c create mode 100644 cpu-cycles/libcpucycles/cpucycles/amd64-tscasm.c create mode 100644 cpu-cycles/libcpucycles/cpucycles/arm32-cortex.c create mode 100644 cpu-cycles/libcpucycles/cpucycles/arm64-pmc.c create mode 100644 cpu-cycles/libcpucycles/cpucycles/arm64-vct.c create mode 100644 cpu-cycles/libcpucycles/cpucycles/cpucycles.h create mode 100644 cpu-cycles/libcpucycles/cpucycles/cpucycles_internal.h create mode 100644 cpu-cycles/libcpucycles/cpucycles/default-gettimeofday.c create mode 100644 cpu-cycles/libcpucycles/cpucycles/default-mach.c create mode 100644 cpu-cycles/libcpucycles/cpucycles/default-monotonic.c create mode 100644 cpu-cycles/libcpucycles/cpucycles/default-perfevent.c create mode 100644 cpu-cycles/libcpucycles/cpucycles/default-zero.c create mode 100644 cpu-cycles/libcpucycles/cpucycles/mips64-cc.c create mode 100644 cpu-cycles/libcpucycles/cpucycles/options create mode 100644 cpu-cycles/libcpucycles/cpucycles/ppc32-mftb.c create mode 100644 cpu-cycles/libcpucycles/cpucycles/ppc64-mftb.c create mode 100644 cpu-cycles/libcpucycles/cpucycles/riscv32-rdcycle.c create mode 100644 cpu-cycles/libcpucycles/cpucycles/riscv64-rdcycle.c create mode 100644 cpu-cycles/libcpucycles/cpucycles/s390x-stckf.c create mode 100644 cpu-cycles/libcpucycles/cpucycles/sparc64-rdtick.c create mode 100644 cpu-cycles/libcpucycles/cpucycles/wrapper.c create mode 100644 cpu-cycles/libcpucycles/cpucycles/x86-tsc.c create mode 100644 cpu-cycles/libcpucycles/cpucycles/x86-tscasm.c create mode 100644 cpu-cycles/libcpucycles/doc/api.md create mode 100644 cpu-cycles/libcpucycles/doc/counters.md create mode 100644 cpu-cycles/libcpucycles/doc/download.md create mode 100644 cpu-cycles/libcpucycles/doc/html/api.html create mode 100644 cpu-cycles/libcpucycles/doc/html/counters.html create mode 100644 cpu-cycles/libcpucycles/doc/html/download.html create mode 100644 cpu-cycles/libcpucycles/doc/html/index.html create mode 100644 cpu-cycles/libcpucycles/doc/html/install.html create mode 100644 cpu-cycles/libcpucycles/doc/html/security.html create mode 100644 cpu-cycles/libcpucycles/doc/html/selection.html create mode 100644 cpu-cycles/libcpucycles/doc/install.md create mode 100644 cpu-cycles/libcpucycles/doc/man/cpucycles.3 create mode 100644 cpu-cycles/libcpucycles/doc/readme.md create mode 100644 cpu-cycles/libcpucycles/doc/security.md create mode 100644 cpu-cycles/libcpucycles/doc/selection.md create mode 100755 cpu-cycles/libcpucycles/scripts-build/install create mode 100755 cpu-cycles/libcpucycles/scripts-build/staticlib create mode 100644 cpu-cycles/libcpucycles/version create mode 100644 cpu-cycles/src/bindings.rs create mode 100644 cpu-cycles/src/lib.rs diff --git a/.gitignore b/.gitignore index 82f4286297..89ef98427f 100644 --- a/.gitignore +++ b/.gitignore @@ -41,3 +41,4 @@ storybook-static envs/qwerty.env .parcel-cache **/.DS_Store +cpu-cycles/libcpucycles/build \ No newline at end of file diff --git a/Cargo.lock b/Cargo.lock index 662c55f585..e168a3d9b8 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -886,6 +886,14 @@ dependencies = [ "uint", ] +[[package]] +name = "cpu-cycles" +version = "0.1.0" +dependencies = [ + "cfg-if", + "libc", +] + [[package]] name = "cpufeatures" version = "0.2.5" @@ -3495,6 +3503,7 @@ dependencies = [ "bs58", "clap 4.1.11", "colored", + "cpu-cycles", "cupid", "dirs", "futures", diff --git a/cpu-cycles/Cargo.toml b/cpu-cycles/Cargo.toml new file mode 100644 index 0000000000..59d2ed52b6 --- /dev/null +++ b/cpu-cycles/Cargo.toml @@ -0,0 +1,12 @@ +[package] +name = "cpu-cycles" +version = "0.1.0" +edition = "2021" +build = "build.rs" +links = "cpucycles" + +[dependencies] +libc = "0.2.140" + +[build-dependencies] +cfg-if = "1" \ No newline at end of file diff --git a/cpu-cycles/build.rs b/cpu-cycles/build.rs new file mode 100644 index 0000000000..d86a4f6df3 --- /dev/null +++ b/cpu-cycles/build.rs @@ -0,0 +1,65 @@ +use std::{env, path::PathBuf, process::Command}; + +fn main() { + let out_dir = env::var("OUT_DIR").unwrap(); + let out_path = PathBuf::from(&out_dir); + let source_path = PathBuf::from("libcpucycles") + .canonicalize() + .expect("cannot canonicalize path"); + + cfg_if::cfg_if! { + if #[cfg(not(any(target_arch = "x86", target_arch = "x86_64", target_arch = "mips", target_arch = "powerpc", target_arch = "powerpc64", target_arch = "arm")))] { + panic!("Unsupported architecture - {}!", env::var("CARGO_CFG_TARGET_ARCH").unwrap(), ) + } + }; + + let mut compile_o_command = Command::new("./configure"); + let compile_o_command = compile_o_command + .current_dir(&source_path) + .arg(format!("--prefix={out_dir}")); + + match compile_o_command.output() { + Ok(output) => { + if !output.status.success() { + panic!("{:?}", unsafe { + std::str::from_utf8_unchecked(&output.stderr) + }) + } + } + Err(e) => panic!("{e}"), + } + + let mut compile_o_command = Command::new("make"); + let compile_o_command = compile_o_command.current_dir(&source_path).arg("install"); + + match compile_o_command.output() { + Ok(output) => { + if !output.status.success() { + panic!("{:?}", unsafe { + std::str::from_utf8_unchecked(&output.stderr) + }) + } + } + Err(e) => panic!("{e}"), + } + + println!( + "cargo:rustc-link-search=native={}", + out_path.join("lib").to_str().unwrap() + ); + println!("cargo:rustc-link-lib=static=cpucycles"); + + let mut compile_o_command = Command::new("make"); + let compile_o_command = compile_o_command.current_dir(source_path).arg("clean"); + + match compile_o_command.output() { + Ok(output) => { + if !output.status.success() { + panic!("{:?}", unsafe { + std::str::from_utf8_unchecked(&output.stderr) + }) + } + } + Err(e) => panic!("{e}"), + } +} diff --git a/cpu-cycles/libcpucycles/Makefile b/cpu-cycles/libcpucycles/Makefile new file mode 100644 index 0000000000..8968a9bf80 --- /dev/null +++ b/cpu-cycles/libcpucycles/Makefile @@ -0,0 +1,8 @@ +default: + cd build && $(MAKE) + +install: + cd build && $(MAKE) install + +clean: + cd build && $(MAKE) clean diff --git a/cpu-cycles/libcpucycles/autogen/html b/cpu-cycles/libcpucycles/autogen/html new file mode 100755 index 0000000000..84a0ff2dab --- /dev/null +++ b/cpu-cycles/libcpucycles/autogen/html @@ -0,0 +1,69 @@ +#!/usr/bin/env python3 + +import os +import datetime +import markdown + +def load(fn): + with open(fn) as f: + return f.read() + +style = load('autogen/html-style') +sitetitle = load('autogen/html-title') + +files = [] + +with open('autogen/html-files') as f: + for line in f: + line = line.strip() + line = line.split(':') + if len(line) != 3: continue + files += [line] + +for md,html,pagetitle in files: + fnmd = 'doc/%s.md' % md + fnhtml = 'doc/html/%s.html' % html + output = '' + + x = load(fnmd) + x = markdown.markdown(x,extensions=['markdown.extensions.extra','markdown.extensions.tables']) + mtime = datetime.datetime.utcfromtimestamp(os.path.getmtime(fnmd)).strftime('%Y.%m.%d') + + output += '\n\n' + output += style + output += '\n' + output += pagetitle + output += '\n' + output += '\n' + output += '\n' + + output += '
\n' + output += sitetitle + output += '
\n' + + output += '