Skip to content

Commit e4f3909

Browse files
committed
Add optional event logging
We can use this with Servo to generate traces from real websites for optimization purposes.
1 parent 82e1af0 commit e4f3909

File tree

4 files changed

+58
-4
lines changed

4 files changed

+58
-4
lines changed

Cargo.toml

+3
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@ name = "string_cache"
44
version = "0.0.0"
55
authors = [ "The Servo Project Developers" ]
66

7+
[features]
8+
log-events = []
9+
710
[dependencies.string_cache_macros]
811
path = "macros"
912

src/atom/mod.rs

+12-3
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,12 @@ use sync::Mutex;
2828

2929
use self::repr::{UnpackedAtom, Static, Inline, Dynamic};
3030

31+
#[cfg(feature = "log-events")]
32+
use event;
33+
34+
#[cfg(not(feature = "log-events"))]
35+
macro_rules! log (($e:expr) => (()))
36+
3137
#[path="../../shared/repr.rs"]
3238
pub mod repr;
3339

@@ -110,6 +116,7 @@ impl StringCache {
110116
StringCacheEntry::new(self.buckets[bucket_index], hash, string_to_add));
111117
}
112118
self.buckets[bucket_index] = ptr;
119+
log!(event::Insert(ptr as u64, String::from_str(string_to_add)));
113120
}
114121

115122
debug_assert!(ptr != ptr::null_mut());
@@ -146,6 +153,8 @@ impl StringCache {
146153
heap::deallocate(ptr as *mut u8,
147154
mem::size_of::<StringCacheEntry>(), ENTRY_ALIGNMENT);
148155
}
156+
157+
log!(event::Remove(key));
149158
}
150159
}
151160

@@ -180,9 +189,9 @@ impl Atom {
180189
}
181190
};
182191

183-
Atom {
184-
data: unsafe { unpacked.pack() },
185-
}
192+
let data = unsafe { unpacked.pack() };
193+
log!(event::Intern(data))
194+
Atom { data: data }
186195
}
187196

188197
pub fn as_slice<'t>(&'t self) -> &'t str {

src/event.rs

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
// Copyright 2014 The Servo Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution.
3+
//
4+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
5+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
6+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
7+
// option. This file may not be copied, modified, or distributed
8+
// except according to those terms.
9+
10+
#![macro_escape]
11+
12+
use core::prelude::*;
13+
14+
use alloc::boxed::Box;
15+
use collections::MutableSeq;
16+
use collections::vec::Vec;
17+
use collections::string::String;
18+
use sync::Mutex;
19+
20+
#[deriving(PartialEq, Eq, PartialOrd, Ord, Hash, Clone, Show, Encodable)]
21+
pub enum Event {
22+
Intern(u64),
23+
Insert(u64, String),
24+
Remove(u64),
25+
}
26+
27+
lazy_static! {
28+
pub static ref LOG: Mutex<Vec<Event>>
29+
= Mutex::new(Vec::with_capacity(50_000));
30+
}
31+
32+
pub fn log(e: Event) {
33+
LOG.lock().push(e);
34+
}
35+
36+
macro_rules! log (($e:expr) => (::event::log($e)))

src/lib.rs

+7-1
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,15 @@ extern crate xxhash;
4141
#[phase(plugin)]
4242
extern crate string_cache_macros;
4343

44+
#[cfg(feature = "log-events")]
45+
extern crate serialize;
46+
4447
pub use atom::Atom;
4548
pub use namespace::{Namespace, QualName};
4649

50+
#[cfg(feature = "log-events")]
51+
pub mod event;
52+
4753
pub mod atom;
4854
pub mod namespace;
4955

@@ -60,7 +66,7 @@ mod string_cache {
6066
// For macros and deriving.
6167
#[cfg(not(test))]
6268
mod std {
63-
pub use core::{cmp, fmt, clone, option, mem};
69+
pub use core::{cmp, fmt, clone, option, mem, result};
6470
pub use collections::hash;
6571

6672
pub mod sync {

0 commit comments

Comments
 (0)