Skip to content

Commit

Permalink
fix logging to compile on windows (#109)
Browse files Browse the repository at this point in the history
* use page_size crate

* fix file resizing while mapped

* remove .close()

* probably more ergonomic way to drop

---------

Co-authored-by: Your Name <[email protected]>
  • Loading branch information
Lishen1 and Your Name authored Nov 24, 2024
1 parent 33bc4ea commit 2e8d9af
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 17 deletions.
3 changes: 2 additions & 1 deletion core/cu29_unifiedlog/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ repository.workspace = true
cu29-traits = { workspace = true }
bincode = { workspace = true }
memmap2 = "0.9.5"
libc = "0.2.159"
libc = "0.2.164"
page_size = "0.6.0"

[dev-dependencies]
tempfile = { workspace = true }
Expand Down
30 changes: 14 additions & 16 deletions core/cu29_unifiedlog/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use memmap2::{Mmap, MmapMut};
use std::fmt::{Debug, Formatter};
use std::fs::{File, OpenOptions};
use std::io::Read;
use std::mem::ManuallyDrop;
use std::path::{Path, PathBuf};
use std::slice::from_raw_parts_mut;
use std::sync::{Arc, Mutex};
Expand Down Expand Up @@ -193,7 +194,7 @@ impl UnifiedLoggerBuilder {
}

pub fn build(self) -> io::Result<UnifiedLogger> {
let page_size = unsafe { libc::sysconf(libc::_SC_PAGESIZE) as usize };
let page_size = page_size::get();

if self.write && self.create {
let ulw = UnifiedLoggerWrite::new(
Expand Down Expand Up @@ -224,7 +225,7 @@ pub struct UnifiedLoggerRead {

struct SlabEntry {
file: File,
mmap_buffer: MmapMut,
mmap_buffer: ManuallyDrop<MmapMut>,
current_global_position: usize,
sections_offsets_in_flight: Vec<usize>,
flushed_until_offset: usize,
Expand All @@ -233,7 +234,12 @@ struct SlabEntry {

impl Drop for SlabEntry {
fn drop(&mut self) {
self.close();
self.flush_until(self.current_global_position);
unsafe { ManuallyDrop::drop(&mut self.mmap_buffer) };
self.file
.set_len(self.current_global_position as u64)
.expect("Failed to trim datalogger file");

if !self.sections_offsets_in_flight.is_empty() {
eprintln!("Error: Slab not full flushed.");
}
Expand All @@ -247,7 +253,8 @@ pub enum AllocatedSection {

impl SlabEntry {
fn new(file: File, page_size: usize) -> Self {
let mmap_buffer = unsafe { MmapMut::map_mut(&file).expect("Failed to map file") };
let mmap_buffer =
ManuallyDrop::new(unsafe { MmapMut::map_mut(&file).expect("Failed to map file") });
Self {
file,
mmap_buffer,
Expand Down Expand Up @@ -276,7 +283,7 @@ impl SlabEntry {
fn is_it_my_section(&self, section: &SectionHandle) -> bool {
(section.buffer.as_ptr() >= self.mmap_buffer.as_ptr())
&& (section.buffer.as_ptr() as usize)
< (self.mmap_buffer.as_ptr() as usize + self.mmap_buffer.len())
< (self.mmap_buffer.as_ref().as_ptr() as usize + self.mmap_buffer.as_ref().len())
}

/// Flush the section to disk.
Expand Down Expand Up @@ -359,13 +366,6 @@ impl SlabEntry {
AllocatedSection::Section(SectionHandle::create(section_header, handle_buffer))
}

fn close(&mut self) {
self.flush_until(self.current_global_position);
self.file
.set_len(self.current_global_position as u64)
.expect("Failed to trim datalogger file");
}

#[cfg(test)]
fn used(&self) -> usize {
self.current_global_position
Expand Down Expand Up @@ -505,7 +505,6 @@ impl UnifiedLoggerWrite {
fn garbage_collect_backslabs(&mut self) {
self.back_slabs.retain_mut(|slab| {
if slab.sections_offsets_in_flight.is_empty() {
slab.close();
false
} else {
true
Expand Down Expand Up @@ -737,7 +736,6 @@ impl Read for UnifiedLoggerIOReader {
mod tests {
use super::*;
use bincode::decode_from_reader;
use libc::{sysconf, _SC_PAGESIZE};
use std::io::BufReader;
use std::path::PathBuf;
use tempfile::TempDir;
Expand Down Expand Up @@ -782,8 +780,8 @@ mod tests {
logger.add_section(UnifiedLogType::StructuredLogLine, 1024);
logger.add_section(UnifiedLogType::CopperList, 2048);
let used = logger.front_slab.used();
assert!(used < 4 * unsafe { sysconf(_SC_PAGESIZE) as usize }); // ie. 3 headers, 1 page max per
// logger drops
assert!(used < 4 * page_size::get()); // ie. 3 headers, 1 page max per
// logger drops

used
};
Expand Down

0 comments on commit 2e8d9af

Please sign in to comment.