Advertisement
Guest User

Untitled

a guest
Apr 26th, 2020
274
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Go 2.90 KB | None | 0 0
  1. // ragel -G2 -Z matcher.r6
  2.  
  3. --- 1   2020-04-27 01:51:27.000000000 +0300
  4. +++ matcher.r6  2020-04-27 01:45:58.000000000 +0300
  5. @@ -0,0 +1,18 @@
  6. +package main
  7. +
  8. +func MatchDate(data []byte) bool {
  9. +    %% machine lexer;
  10. +    %% write data;
  11. +
  12. +    cs, p, pe, eof := 0, 0, len(data), len(data)
  13. +    _ = eof
  14. +
  15. +    %%{
  16. +        main := ^'#' any* '#' ('1'|'5'){2} '111' '0'+ any* @{ return true } ;
  17. +
  18. +        write init;
  19. +        write exec;
  20. +    }%%
  21. +
  22. +    return false
  23. +}
  24.  
  25. // go run parallelfast.go matcher.go
  26.  
  27. --- 1   2020-04-27 01:51:27.000000000 +0300
  28. +++ parallelfast.go 2020-04-27 01:46:37.000000000 +0300
  29. @@ -0,0 +1,111 @@
  30. +package main
  31. +
  32. +import (
  33. +   "fmt"
  34. +   "log"
  35. +   "os"
  36. +// "regexp"
  37. +   "sync"
  38. +   "syscall"
  39. +   "time"
  40. +   "unsafe"
  41. +)
  42. +
  43. +const maxSize int64 = 1 << 36
  44. +const numberWorkers = 32
  45. +const lineSize = 65
  46. +
  47. +var waitGroup sync.WaitGroup
  48. +//var regex = regexp.MustCompile("^[^#]*#[15][15]1110+$")
  49. +
  50. +func main() {
  51. +   start := time.Now()
  52. +   log.Printf("Started: %v", start)
  53. +
  54. +   BenchP()
  55. +
  56. +   fmt.Printf("Elapsed time: %v\n", time.Since(start))
  57. +}
  58. +
  59. +func ReadLines(mmap []byte, size int64) [][]byte {
  60. +   var lines [][]byte
  61. +   last_char := int64(0)
  62. +   for i := int64(0); i < size; i++ {
  63. +       if mmap[i] == '\n' {
  64. +           lines = append(lines, mmap[last_char:i])
  65. +           last_char = i + 1
  66. +           i += lineSize
  67. +       }
  68. +   }
  69. +
  70. +   return lines
  71. +}
  72. +
  73. +func QueueLines(queue chan<- [][]byte, lines [][]byte) {
  74. +   linesSz := len(lines)
  75. +   for i := 0; i < linesSz; i += 128 {
  76. +       queue <- lines[i : i+128]
  77. +   }
  78. +}
  79. +
  80. +func BenchP() {
  81. +   ptrSz := int64(unsafe.Sizeof(uint16(0)))
  82. +
  83. +   fd, err := os.Open("log-sample.txt")
  84. +   if err != nil {
  85. +       log.Fatal("Error opening input file:", err)
  86. +   }
  87. +   defer fd.Close()
  88. +
  89. +   fdStat, err := fd.Stat()
  90. +   if err != nil {
  91. +       log.Fatal(err)
  92. +   }
  93. +
  94. +   size := fdStat.Size()
  95. +
  96. +   waitGroup.Add(numberWorkers)
  97. +
  98. +   queue := make(chan [][]byte, size/lineSize)
  99. +   for gr := 1; gr <= numberWorkers; gr++ {
  100. +       go worker(queue, gr, &waitGroup)
  101. +   }
  102. +
  103. +   var mmap []byte
  104. +   mmap, err = syscall.Mmap(int(fd.Fd()), 0, int(size*ptrSz), syscall.PROT_READ, syscall.MAP_SHARED)
  105. +
  106. +   if err = madvise(mmap, syscall.MADV_SEQUENTIAL|syscall.MADV_WILLNEED); err != nil {
  107. +       log.Fatal(err)
  108. +   }
  109. +
  110. +   lines := ReadLines(mmap, size)
  111. +   QueueLines(queue, lines)
  112. +
  113. +   close(queue)
  114. +   waitGroup.Wait()
  115. +}
  116. +
  117. +func madvise(b []byte, advice int) (err error) {
  118. +   _, _, e1 := syscall.Syscall(syscall.SYS_MADVISE, uintptr(unsafe.Pointer(&b[0])), uintptr(len(b)), uintptr(advice))
  119. +   if e1 != 0 {
  120. +       err = e1
  121. +   }
  122. +   return
  123. +}
  124. +
  125. +func worker(queue <-chan [][]byte, id int, wg *sync.WaitGroup) {
  126. +   defer wg.Done()
  127. +   for {
  128. +       lines, ok := <-queue
  129. +       if !ok {
  130. +           fmt.Printf("Worker: %d : Shutting Down\n", id)
  131. +           return
  132. +       }
  133. +       for _, line := range lines {
  134. +//         if regex.Match(line) {
  135. +            if MatchDate(line) {
  136. +               fmt.Printf("[%v] Match: %v\n", id, string(line))
  137. +           }
  138. +       }
  139. +   }
  140. +}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement