Guest User

Talk outline

a guest
Aug 12th, 2016
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.51 KB | None | 0 0
  1. ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
  2. TESTING NATIVE BINARIES USING CFFI
  3.  
  4. Noufal Ibrahim
  5. ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
  6.  
  7.  
  8. Table of Contents
  9. ─────────────────
  10.  
  11. 1 Section 1 : Introduction to FFI in python (approx. 7 minutes)
  12. .. 1.1 Calling code from across languages
  13. .. 1.2 Ctypes in python
  14. ..... 1.2.1 Standard library
  15. ..... 1.2.2 Handles most things.
  16. ..... 1.2.3 Directly calls functions from compiled files.
  17. ..... 1.2.4 A simple example
  18. ..... 1.2.5 TODO Problems with ctypes
  19. .. 1.3 CFFI
  20. ..... 1.3.1 A project by the pypy team
  21. ..... 1.3.2 More flexible and fixes several issues with ctypes.
  22. ..... 1.3.3 Simple example
  23. ..... 1.3.4 TODO Different ways of using the library
  24. ..... 1.3.5 Almost obviates raw C extensions
  25. ..... 1.3.6 TODO Advantages with pypy
  26. 2 Section 2 : My use case (approx. 15 minutes)
  27. .. 2.1 cpslib
  28. ..... 2.1.1 A low level C rewrite of psutil
  29. ..... 2.1.2 Cross platform (currently supports OSX and Linux - Windows support in the works).
  30. ..... 2.1.3 A simple API example
  31. .. 2.2 cpslib python bindings using cffi
  32. ..... 2.2.1 TODO `cffi_modules' in setup.py
  33. ..... 2.2.2 Example usage (Demo)
  34. 3 Section 3 : Writing tests in python (approx 5 minutes)
  35. .. 3.1 Now, we can write tests for this
  36. .. 3.2 This validates the C implementation against psutils
  37. .. 3.3 TODO Example of doing the same thing with rust libraries (if time permits)
  38. 4 Section 4 : Wrap up and Q/A
  39. .. 4.1 Stuff that I've skipped
  40.  
  41.  
  42.  
  43.  
  44.  
  45. 1 Section 1 : Introduction to FFI in python (approx. 7 minutes)
  46. ═══════════════════════════════════════════════════════════════
  47.  
  48. 1.1 Calling code from across languages
  49. ──────────────────────────────────────
  50.  
  51. • Loose coupling (e.g. XML-RPC, RESTful services)
  52. • Slow
  53. • Serialisation
  54. • Infrastructure
  55. • Common layers (e.g. languages that run on the JVM)
  56. • Some minor API quirks
  57. • But generally great
  58. • I have no experience with this.
  59. • Calling native binaries
  60. • FFI. libffi
  61. • Issues - Translation of types, ABIs etc.
  62. • Different from things like Cython etc. (which compile
  63.  
  64.  
  65. 1.2 Ctypes in python
  66. ────────────────────
  67.  
  68. 1.2.1 Standard library
  69. ╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌
  70.  
  71.  
  72. 1.2.2 Handles most things.
  73. ╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌
  74.  
  75.  
  76. 1.2.3 Directly calls functions from compiled files.
  77. ╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌
  78.  
  79.  
  80. 1.2.4 A simple example
  81. ╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌
  82.  
  83. ┌────
  84. │ int
  85. │ add(int x, int y)
  86. │ {
  87. │ return x + y;
  88. │ }
  89. └────
  90.  
  91. ┌────
  92. │ gcc -c -o example.o example.c
  93. │ gcc -shared -o example.so example.o
  94. └────
  95.  
  96. ┌────
  97. │ import ctypes
  98. │ ctypes.CDLL("./example.so")
  99. │ print (example.add(4, 5)) # Prints 9
  100. └────
  101.  
  102.  
  103. 1.2.5 TODO Problems with ctypes
  104. ╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌
  105.  
  106.  
  107. 1.3 CFFI
  108. ────────
  109.  
  110. 1.3.1 A project by the pypy team
  111. ╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌
  112.  
  113.  
  114. 1.3.2 More flexible and fixes several issues with ctypes.
  115. ╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌
  116.  
  117.  
  118. 1.3.3 Simple example
  119. ╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌
  120.  
  121. ┌────
  122. │ f = cffi.FFI()
  123. │ f.cdef("int add(int, int);")
  124. │ ffi = f.dlopen("./example.so")
  125. │ ffi.add(4, 5) # prints 9
  126. └────
  127.  
  128.  
  129. 1.3.4 TODO Different ways of using the library
  130. ╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌
  131.  
  132.  
  133. 1.3.5 Almost obviates raw C extensions
  134. ╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌
  135.  
  136.  
  137. 1.3.6 TODO Advantages with pypy
  138. ╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌
  139.  
  140.  
  141. 2 Section 2 : My use case (approx. 15 minutes)
  142. ══════════════════════════════════════════════
  143.  
  144. 2.1 cpslib
  145. ──────────
  146.  
  147. 2.1.1 A low level C rewrite of psutil
  148. ╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌
  149.  
  150. • [https://github.com/nibrahim/cpslib]
  151. • Contributions welcome.
  152.  
  153.  
  154. 2.1.2 Cross platform (currently supports OSX and Linux - Windows support in the works).
  155. ╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌
  156.  
  157.  
  158. 2.1.3 A simple API example
  159. ╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌
  160.  
  161. ┌────
  162. │ uint32_t cpu_count(bool);
  163. │ void
  164. │ test_cpu_count() {
  165. │ uint32_t logical = cpu_count(true);
  166. │ uint32_t physical = cpu_count(false);
  167. │ printf("Logical : %" PRIu32 "\nPhysical : %" PRIu32 "\n", logical, physical);
  168. │ printf("\n");
  169. │ }
  170. └────
  171.  
  172.  
  173. 2.2 cpslib python bindings using cffi
  174. ─────────────────────────────────────
  175.  
  176. 2.2.1 TODO `cffi_modules' in setup.py
  177. ╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌
  178.  
  179.  
  180. 2.2.2 Example usage (Demo)
  181. ╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌
  182.  
  183. ◊ 2.2.2.1 In C
  184.  
  185. ┌────
  186. │ #include <inttypes.h>
  187. │ #include <stdio.h>
  188. │ #include "pslib.h"
  189. │ int
  190. │ main()
  191. │ {
  192. │ uint32_t logical = cpu_count(true);
  193. │ printf("Number of logical CPUs %" PRIu32 "\n", logical);
  194. │ return 0;
  195. │ }
  196. └────
  197.  
  198.  
  199. ◊ 2.2.2.2 In Python
  200.  
  201. ┌────
  202. │ from pycpslib import lib as P
  203. │ print ("Number of logical CPUs {}".format(P.cpu_count(True)))
  204. └────
  205.  
  206.  
  207. ◊ 2.2.2.2.1
  208.  
  209.  
  210. 3 Section 3 : Writing tests in python (approx 5 minutes)
  211. ════════════════════════════════════════════════════════
  212.  
  213. 3.1 Now, we can write tests for this
  214. ────────────────────────────────────
  215.  
  216.  
  217. 3.2 This validates the C implementation against psutils
  218. ───────────────────────────────────────────────────────
  219.  
  220. ┌────
  221. │ from pycpslib import lib as P
  222. │ import psutil
  223. │ def test_cpu_count(flush):
  224. │ assert P.cpu_count(1) == psutil.cpu_count(True), "Mismatch in number of logical CPUs"
  225. │ assert P.cpu_count(0) == psutil.cpu_count(False), "Mismatch in number of physical CPUs"
  226. └────
  227.  
  228.  
  229. 3.3 TODO Example of doing the same thing with rust libraries (if time permits)
  230. ──────────────────────────────────────────────────────────────────────────────
  231.  
  232.  
  233. 4 Section 4 : Wrap up and Q/A
  234. ═════════════════════════════
  235.  
  236. 4.1 Stuff that I've skipped
  237. ───────────────────────────
  238.  
  239. • APIs to pass in and return compound data types (arrays, structures
  240. etc.)
  241. • Different modes of using the library
  242. • Q/A
Add Comment
Please, Sign In to add comment