Skip to content

Instantly share code, notes, and snippets.

@ecin
Created August 31, 2012 17:34

Revisions

  1. ecin revised this gist Sep 2, 2012. No changes.
  2. ecin revised this gist Sep 2, 2012. 2 changed files with 9 additions and 0 deletions.
    File renamed without changes.
    9 changes: 9 additions & 0 deletions spectator.d
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,9 @@
    golang*::Ping:
    {
    printf("Ping hits, counter goes up to #%d!", arg0);
    }

    golang*::Pong:
    {
    printf("Pong connects, counter goes down to #%d!", arg0);
    }
  3. ecin revised this gist Sep 2, 2012. 4 changed files with 22 additions and 11 deletions.
    8 changes: 4 additions & 4 deletions probes.c
    Original file line number Diff line number Diff line change
    @@ -7,16 +7,16 @@
    */
    #include "probes.h"

    void Ping() {
    GOLANG_PING();
    void Ping(int n) {
    GOLANG_PING(n);
    }

    int Ping_enabled() {
    return GOLANG_PING_ENABLED();
    }

    void Pong() {
    GOLANG_PONG();
    void Pong(int n) {
    GOLANG_PONG(n);
    }

    int Pong_enabled() {
    4 changes: 2 additions & 2 deletions probes.d
    Original file line number Diff line number Diff line change
    @@ -3,6 +3,6 @@
    dtrace -o probes.h -h -s probes.d
    */
    provider golang {
    probe ping();
    probe pong();
    probe ping(int);
    probe pong(int);
    };
    17 changes: 14 additions & 3 deletions probes.go
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,10 @@
    package main

    /*
    Ping vs Pong: A Gladiatorial Match
    Two goroutines enter, only one leaves...
    */

    /*
    #cgo LDFLAGS: -lprobes -L/usr/local/lib
    #include "probes.h"
    @@ -13,6 +18,7 @@ import (

    func ping(c chan int, q chan int) {
    for ;; {
    // Prepare a swing
    time.Sleep(time.Duration(rand.Intn(5)) * time.Second)
    n := <- c
    n += 1
    @@ -22,12 +28,14 @@ func ping(c chan int, q chan int) {
    }

    c <- n
    // Dtrace spectators: roar for Ping!
    C.Ping(C.int(n))
    }
    }

    func pong(c chan int, q chan int) {
    for ;; {
    // Charge an attack
    time.Sleep(time.Duration(rand.Intn(5)) * time.Second)
    n := <- c
    n -= 1
    @@ -37,6 +45,7 @@ func pong(c chan int, q chan int) {
    }

    c <- n
    // Dtrace spectators: cheer for Pong!
    C.Pong(C.int(n))
    }
    }
    @@ -45,17 +54,19 @@ func main() {
    c := make(chan int, 2)
    q := make(chan int)

    // Ping and Pong enter the arena
    go ping(c, q)
    go pong(c, q)

    // The match starts!
    c <- 5

    // Wait for quit
    // Wait for victor signal
    winner := <- q

    if winner == 0 {
    println("Ping wins!")
    println("After a long and arduous battle, Pong falls to its knees, its body a weary mess. Ping wins!")
    } else {
    println("Pong wins!")
    println("Overcome by one too many wounds, Ping rushes to the ground. Pong wins!")
    }
    }
    4 changes: 2 additions & 2 deletions probes.h
    Original file line number Diff line number Diff line change
    @@ -2,7 +2,7 @@
    /* ... */

    /* probes.c functions */
    void Ping();
    void Ping(int);
    int Ping_enabled();
    void Pong();
    void Pong(int);
    int Pong_enabled();
  4. ecin revised this gist Sep 2, 2012. 1 changed file with 61 additions and 0 deletions.
    61 changes: 61 additions & 0 deletions probes.go
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,61 @@
    package main

    /*
    #cgo LDFLAGS: -lprobes -L/usr/local/lib
    #include "probes.h"
    */
    import "C"

    import (
    "time"
    "math/rand"
    )

    func ping(c chan int, q chan int) {
    for ;; {
    time.Sleep(time.Duration(rand.Intn(5)) * time.Second)
    n := <- c
    n += 1

    if n == 10 {
    q <- 0
    }

    c <- n
    C.Ping(C.int(n))
    }
    }

    func pong(c chan int, q chan int) {
    for ;; {
    time.Sleep(time.Duration(rand.Intn(5)) * time.Second)
    n := <- c
    n -= 1

    if n == 0 {
    q <- 1
    }

    c <- n
    C.Pong(C.int(n))
    }
    }

    func main() {
    c := make(chan int, 2)
    q := make(chan int)

    go ping(c, q)
    go pong(c, q)

    c <- 5

    // Wait for quit
    winner := <- q

    if winner == 0 {
    println("Ping wins!")
    } else {
    println("Pong wins!")
    }
    }
  5. ecin revised this gist Aug 31, 2012. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions probes.h
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,7 @@
    /* Header file generated by dtrace */
    /* ... */

    /* probes.c functions */
    void Ping();
    int Ping_enabled();
    void Pong();
  6. ecin revised this gist Aug 31, 2012. 1 changed file with 7 additions and 0 deletions.
    7 changes: 7 additions & 0 deletions probes.h
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,7 @@
    /* Header file generated by dtrace */
    /* ... */

    void Ping();
    int Ping_enabled();
    void Pong();
    int Pong_enabled();
  7. ecin revised this gist Aug 31, 2012. 1 changed file with 10 additions and 3 deletions.
    13 changes: 10 additions & 3 deletions probes.c
    Original file line number Diff line number Diff line change
    @@ -1,17 +1,24 @@
    /*
    Compile probes.c as a shared library
    In Mac OS X (http://www.finkproject.org/doc/porting/shared.php#build-lib):
    cc -fno-common -c probes.c
    cc -dynamiclib -install_name /usr/local/lib/libprobes.dylib -o libprobes.dylib probes.o
    cp libprobes.dylib /usr/local/lib/
    */
    #include "probes.h"

    void Ping() {
    GOLANG_PING();
    }

    bool Ping_enabled() {
    int Ping_enabled() {
    return GOLANG_PING_ENABLED();
    }

    void Pong() {
    GOLANG_PONG();
    }

    bool Pong_enabled() {
    int Pong_enabled() {
    return GOLANG_PONG_ENABLED();
    }
    }
  8. ecin revised this gist Aug 31, 2012. 1 changed file with 17 additions and 0 deletions.
    17 changes: 17 additions & 0 deletions probes.c
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,17 @@
    #include "probes.h"

    void Ping() {
    GOLANG_PING();
    }

    bool Ping_enabled() {
    return GOLANG_PING_ENABLED();
    }

    void Pong() {
    GOLANG_PONG();
    }

    bool Pong_enabled() {
    return GOLANG_PONG_ENABLED();
    }
  9. ecin created this gist Aug 31, 2012.
    8 changes: 8 additions & 0 deletions probes.d
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,8 @@
    /*
    Generate probes.h header file with:
    dtrace -o probes.h -h -s probes.d
    */
    provider golang {
    probe ping();
    probe pong();
    };