CloogleServer.icl 13.4 KB
Newer Older
1 2
module CloogleServer

3 4 5
import StdArray
import StdBool
import StdFile
Camil Staps's avatar
Camil Staps committed
6
from StdFunc import id, o
Camil Staps's avatar
Camil Staps committed
7
import StdList
8
import StdMisc
9 10
import StdOrdList
import StdOverloaded
11
import StdString
12
import StdTuple
13

14
from TCPIP import :: IPAddress, :: Port, instance toString IPAddress
15

16 17 18 19
import Clean.Types
import Clean.Types.Parse
import Clean.Types.Unify
import Clean.Types.Util
20
import Control.Applicative
21
import Control.Monad => qualified join
22
import Data.Error
23
import qualified Data.Foldable as Foldable
Camil Staps's avatar
Camil Staps committed
24
from Data.Foldable import class Foldable
25
from Data.Func import $, hyperstrict, instance Functor ((->) r), mapSt, seqSt, on, `on`
26
import Data.Functor
Camil Staps's avatar
Camil Staps committed
27
from Data.List import permutations
28
import Data.Maybe
29
import Data.Tuple
Camil Staps's avatar
Camil Staps committed
30
import System._Posix
31
import System.CommandLine
32
import System.Options
Camil Staps's avatar
Camil Staps committed
33
import System.Time
34
from Text import class Text(concat,join,toLowerCase), instance Text String, <+
35
import Text.GenJSON
Camil Staps's avatar
Camil Staps committed
36

Camil Staps's avatar
Camil Staps committed
37 38 39 40 41 42 43
import Cloogle.API
import Cloogle.DB
import Cloogle.Search

import Util.SimpleTCPServer
import Util.Cache
import Util.Memory
Camil Staps's avatar
Camil Staps committed
44

45 46
MAX_RESULTS        :== 15
CACHE_PREFETCH     :== 5
Camil Staps's avatar
Camil Staps committed
47
CACHE_NS_THRESHOLD :== 20000000
48

49
:: RequestCacheKey
Camil Staps's avatar
Camil Staps committed
50 51 52 53
	= { c_unify            :: Maybe Type
	  , c_name             :: Maybe String
	  , c_className        :: Maybe String
	  , c_typeName         :: Maybe String
54
	  , c_using            :: Maybe [String]
Camil Staps's avatar
Camil Staps committed
55 56 57
	  , c_modules          :: Maybe [String]
	  , c_libraries        :: Maybe [String]
	  , c_include_builtins :: Bool
58
	  , c_include_core     :: Bool
59
	  , c_include_apps     :: Bool
Camil Staps's avatar
Camil Staps committed
60
	  , c_page             :: Int
61 62
	  }

63
derive JSONEncode Kind, Type, RequestCacheKey, TypeRestriction
64
derive JSONDecode Kind, Type, RequestCacheKey, TypeRestriction
65 66 67
instance toString RequestCacheKey
where toString rck = toString $ toJSON rck

68
toRequestCacheKey :: !*CloogleDB !Request -> *(!RequestCacheKey, !*CloogleDB)
Camil Staps's avatar
Camil Staps committed
69
toRequestCacheKey db r
70
# (alwaysUnique,db) = alwaysUniquePredicate db
71
# (allsyns,db) = allTypeSynonyms db
Camil Staps's avatar
Camil Staps committed
72 73
= (
	{ c_unify            = snd <$>
74
		prepare_unification True alwaysUnique allsyns <$>
Camil Staps's avatar
Camil Staps committed
75
		(parseType o fromString =<< r.unify)
76
	, c_name             = toLowerCase <$> r.Request.name
77 78
	, c_className        = r.className
	, c_typeName         = r.typeName
79
	, c_using            = r.using
Camil Staps's avatar
Camil Staps committed
80 81
	, c_modules          = sort <$> r.modules
	, c_libraries        = sort <$> r.libraries
82 83 84 85
	, c_include_builtins = fromMaybe DEFAULT_INCLUDE_BUILTINS r.include_builtins
	, c_include_core     = fromMaybe DEFAULT_INCLUDE_CORE r.include_core
	, c_include_apps     = fromMaybe DEFAULT_INCLUDE_APPS r.include_apps
	, c_page             = fromMaybe 0 r.page
86
	}, db)
87 88
fromRequestCacheKey :: RequestCacheKey -> Request
fromRequestCacheKey k =
Camil Staps's avatar
Camil Staps committed
89
	{ unify            = concat <$> print False <$> unprepare <$> k.c_unify
90 91 92
	, name             = k.c_name
	, className        = k.c_className
	, typeName         = k.c_typeName
93
	, using            = k.c_using
94 95 96 97 98 99 100
	, modules          = k.c_modules
	, libraries        = k.c_libraries
	, include_builtins = Just k.c_include_builtins
	, include_core     = Just k.c_include_core
	, include_apps     = Just k.c_include_apps
	, page             = Just k.c_page
	}
Camil Staps's avatar
Camil Staps committed
101 102 103 104 105 106 107 108 109 110 111 112 113
where
	unprepare :: !Type -> Type
	unprepare (Type t ts) = Type t (map unprepare ts)
	unprepare (Func is t tc) = Func (map unprepare is) (unprepare t) (map unprepareTR tc)
	unprepare (Var tv) = Var (tv % (1,size tv-1))
	unprepare (Cons v ts) = Cons (v % (1,size v-1)) (map unprepare ts)
	unprepare (Uniq t) = Uniq (unprepare t)
	unprepare (Forall ts t tc) = Forall (map unprepare ts) (unprepare t) (map unprepareTR tc)
	unprepare (Arrow mt) = Arrow (unprepare <$> mt)

	unprepareTR :: !TypeRestriction -> TypeRestriction
	unprepareTR (Instance c ts) = Instance c (map unprepare ts)
	unprepareTR (Derivation g t) = Derivation g (unprepare t)
114 115

:: Options =
116 117 118
	{ port         :: !Int
	, reload_cache :: !Bool
	, test_file    :: !Maybe FilePath
Camil Staps's avatar
Camil Staps committed
119
	, test_options :: ![TestOption]
120 121
	}

Camil Staps's avatar
Camil Staps committed
122 123
:: TestOption = NoUnify

124 125 126 127 128 129
instance zero Options
where
	zero =
		{ port         = 31215
		, reload_cache = False
		, test_file    = Nothing
Camil Staps's avatar
Camil Staps committed
130
		, test_options = []
131
		}
132

133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156
optionDescription :: Option Options
optionDescription = WithHelp True $ Options
	[ Shorthand "-p" "--port" $ Option
		"--port"
		(\port opts -> case (toInt port, port) of
			(0, "0") -> Error ["Cannot use port 0"]
			(0, p)   -> Error ["'" <+ p <+ "' is not an integer"]
			(p, _)   -> Ok {Options | opts & port=p})
		"PORT"
		"Listen on port PORT (default: 31215)"
	, Flag
		"--reload-cache"
		(\opts -> Ok {opts & reload_cache=True})
		"Reload the cache in the background"
	, Option
		"--test"
		(\file opts -> Ok {opts & test_file=Just file})
		"FILE"
		"Load queries from FILE and execute them (do not start a TCP server)"
	, Flag
		"--test-no-unify"
		(\opts -> Ok {opts & test_options=[NoUnify:opts.test_options]})
		"Do not test queries that require unification (only used with --test)"
	]
157

158
Start w
159 160
# ([prog:args], w) = getCommandLine w
# opts = parseOptions optionDescription args zero
161 162
| isError opts
	# (io,w) = stdio w
163
	# io = io <<< join "\n" (fromError opts) <<< "\n"
164 165 166
	# (_,w) = fclose io w
	= w
# opts = fromOk opts
Camil Staps's avatar
Camil Staps committed
167 168
# w = disableSwap w
#! (_,f,w) = fopen "types.json" FReadText w
169 170 171 172 173 174 175 176
#! (db,f) = openDB f
#! (ok,db) = isJustU db
| not ok
	# (io,w) = stdio w
	# io = io <<< "Could not open database\n"
	# (_,w) = fclose io w
	= w
#! db = hyperstrict (fromJust db)
177 178 179 180 181 182 183
| isJust opts.test_file
	# (ok,f,w) = fopen (fromJust opts.test_file) FReadText w
	| not ok
		# (io,w) = stdio w
		# io = io <<< "Could not open test file\n"
		# (_,w) = fclose io w
		= w
Camil Staps's avatar
Camil Staps committed
184
	= test opts.test_options f db w
Camil Staps's avatar
Camil Staps committed
185
#! (db,w) = if opts.reload_cache (doInBackground reloadCache) id (db,w)
Camil Staps's avatar
Camil Staps committed
186
#! (_,w) = fclose f w
187
= serve
188
	{ handler           = handle
189
	, logger            = Just log
190
	, port              = opts.Options.port
191 192
	, connect_timeout   = Just 3600000 // 1h
	, keepalive_timeout = Just 5000    // 5s
193
	} db w
194
where
Camil Staps's avatar
Camil Staps committed
195 196 197 198 199 200 201 202 203
	disableSwap :: *World -> *World
	disableSwap w
	# (ok,w) = mlockall (MCL_CURRENT bitor MCL_FUTURE) w
	| ok = w
	# (err,w) = errno w
	# (io,w) = stdio w
	# io = io <<< "Could not lock memory (" <<< err <<< "); process may get swapped out\n"
	= snd $ fclose io w

204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231
handle :: !(Maybe Request) !*CloogleDB !*World -> *(!Response, !(!Maybe CacheKey, !MicroSeconds), !*CloogleDB, !*World)
handle Nothing db w = (err InvalidInput "Couldn't parse input", (Nothing,0), db, w)
handle (Just request=:{unify,name,page}) db w
	#! (start,w) = nsTime w
	//Check cache
	#! (key,db) = toRequestCacheKey db request
	#! (mbResponse, w) = readCache key w
	| isJust mbResponse
		# r = fromJust mbResponse
		= respond start Nothing {r & return = if (r.return == 0) 1 r.return} db w
	| isJust name && size (fromJust name) > 40
		= respond start Nothing (err InvalidName "Function name too long") db w
	| isJust name && any isSpace (fromString $ fromJust name)
		= respond start Nothing (err InvalidName "Name cannot contain spaces") db w
	| isJust unify && isNothing (parseType $ fromString $ fromJust unify)
		= respond start Nothing (err InvalidType "Couldn't parse type") db w
	| all isNothing [unify,name,request.typeName,request.className] && isNothing request.using
		= respond start Nothing (err InvalidInput "Empty query") db w
	// Results
	#! drop_n = fromJust (page <|> pure 0) * MAX_RESULTS
	#! (res,db) = search request db
	#! results = drop drop_n res
	#! more = max 0 (length results - MAX_RESULTS)
	// Suggestions
	#! (suggestions,db) = case unify >>= parseType o fromString of
		Just t -> suggs name t db
		Nothing -> (Nothing, db)
	#! (db,w) = seqSt
232
		(\(_,req,res) (db,w) -> let (k,db`) = toRequestCacheKey db req in (db`,cachePages k CACHE_PREFETCH 0 zero res w))
233 234 235
		(fromMaybe [] suggestions)
		(db,w)
	#! suggestions
236 237 238 239 240
		= sortBy ((<) `on` snd) <$>
			map (\(_,req,reslen) -> (req,reslen)) <$>
			filter (\(always,_,sugres) -> always || sugres >= reslen) <$>
			map (appThd3 length) <$> suggestions
		with reslen = length results
241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263
	#! (results,nextpages) = splitAt MAX_RESULTS results
	// Response
	#! response = if (isEmpty results)
		(err NoResults "No results")
		{ zero
		& data           = results
		, more_available = Just more
		, suggestions    = suggestions
		}
	// Save page prefetches
	#! w = cachePages key CACHE_PREFETCH 1 response nextpages w
	// Save cache file
	= respond start (Just key) response db w
where
	respond :: !Timespec !(Maybe RequestCacheKey) !Response !*CloogleDB !*World ->
		*(!Response, !(!Maybe CacheKey, !MicroSeconds), !*CloogleDB, !*World)
	respond start key r db w
	#! (end,w) = nsTime w
	#! duration = 1000000000 * (end.tv_sec - start.tv_sec) + end.tv_nsec - start.tv_nsec
	#! cache = duration > CACHE_NS_THRESHOLD
	= (r, (if cache (cacheKey <$> key) Nothing, duration / 1000), db, case (cache,key) of
		(True,Just k) -> writeCache LongTerm k r w
		_             -> w)
Camil Staps's avatar
Camil Staps committed
264

265 266 267 268 269 270 271 272 273 274 275 276 277 278
	cachePages :: !RequestCacheKey !Int !Int !Response ![Result] !*World -> *World
	cachePages key _ _  _ [] w = w
	cachePages key 0 _  _ _  w = w
	cachePages key npages i response results w
	# w = writeCache Brief req` resp` w
	= cachePages key (npages - 1) (i + 1) response keep w
	where
		req` = { key & c_page = key.c_page + i }
		resp` =
			{ response
			& more_available = Just $ max 0 (length results - MAX_RESULTS)
			, data = give
			}
		(give,keep) = splitAt MAX_RESULTS results
Mart Lubbers's avatar
Mart Lubbers committed
279

280 281 282 283 284 285 286 287
	suggs :: !(Maybe String) !Type !*CloogleDB -> *(Maybe [(Bool, Request, [Result])], *CloogleDB)
	suggs n t db
	# (swapped, db)     = swap db
	# (capitalized, db) = capitalize db
	# suggestions = case fromMaybe [] swapped ++ [c \\ Just c <- [capitalized]] of
		[] -> Nothing
		ss -> Just ss
	= (suggestions, db)
Camil Staps's avatar
Camil Staps committed
288
	where
289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311
		swap db = case t of
			Func is r cc | length is < 3
				-> appFst Just $ mapSt (\r -> appFst (tuple3 False r) o search r o resetDB) reqs db
				with
					reqs = [{zero & name=n, unify=Just $ concat $ print False $ Func is` r cc}
						\\ is` <- permutations is | is` <> is]
			_ -> (Nothing, db)

		capitalize db = case t` of
			Just t` | t <> t` -> appFst (Just o tuple3 True req) $ search req db
				with req = {zero & name=n, unify=Just $ concat $ print False t`}
			_                 -> (Nothing, db)
		where
			t` = assignAll
				[ ("int",     Type "Int" [])
				, ("bool",    Type "Bool" [])
				, ("char",    Type "Char" [])
				, ("real",    Type "Real" [])
				, ("file",    Type "File" [])
				, ("string",  Type "String" [])
				, ("dynamic", Type "Dynamic" [])
				, ("world",   Uniq (Type "World" []))
				] t
Camil Staps's avatar
Camil Staps committed
312

313 314 315 316 317 318 319 320 321 322 323
reloadCache :: !*(!*CloogleDB, !*World) -> *(!*CloogleDB, !*World)
reloadCache (db,w)
# (ks,w) = allCacheKeys LongTerm w
= loop ks db w
where
	loop :: ![RequestCacheKey] !*CloogleDB !*World -> *(!*CloogleDB, !*World)
	loop [] db w = (db,w)
	loop [k:ks] db w
	# w = removeFromCache LongTerm k w
	# (_,_,db,w) = handle (Just $ fromRequestCacheKey k) db w
	# db = resetDB db
Camil Staps's avatar
Camil Staps committed
324 325
	= loop ks db w

326 327 328 329 330 331 332
doInBackground :: (*a -> *a) *a -> *a
doInBackground f w
#! (pid,w) = fork w
| pid  < 0 = abort "fork failed\n"
| pid  > 0 = w // Parent: return directly
| pid == 0 = snd $ exit 0 $ f w // Child: do function

Camil Staps's avatar
Camil Staps committed
333 334
test :: ![TestOption] !*File !*CloogleDB !*World -> *World
test opts queries db w
335 336 337 338 339 340 341
# (e,queries) = fend queries
| e = w
# (qstring,queries) = freadline queries
# qstring = {c \\ c <-: qstring | c <> '\n' && c <> '\r'}
# q = parseSingleLineRequest qstring
| isError q
	# w = snd $ fclose (stderr <<< "Warning: could not parse '" <<< qstring <<< "'; " <<< fromError q <<< "\n") w
Camil Staps's avatar
Camil Staps committed
342 343 344 345 346 347 348 349 350 351
	= test opts queries db w
# q = fromOk q
| excluded opts q
	= test opts queries db w
# (_,_,db,w) = handle (Just q) db w
= test opts queries db w
where
	excluded :: ![TestOption] !Request -> Bool
	excluded []           _ = False
	excluded [NoUnify:os] r = isJust r.unify || excluded os r
352

353 354 355 356
:: LogMemory =
	{ mem_ip         :: IPAddress
	, mem_time_start :: Tm
	, mem_time_end   :: Tm
357
	, mem_request    :: Maybe Request
358 359 360 361 362 363 364 365 366 367 368
	}

instance zero LogMemory
where
	zero =
		{ mem_ip         = undef
		, mem_time_start = undef
		, mem_time_end   = undef
		, mem_request    = undef
		}

369 370 371
:: MicroSeconds :== Int

:: LogMessage` :== LogMessage (Maybe Request) Response (Maybe CacheKey, MicroSeconds)
372 373 374 375 376

:: LogEntry =
	{ ip            :: String
	, time_start    :: (String, Int)
	, time_end      :: (String, Int)
377
	, microseconds  :: Int
378
	, request       :: Maybe Request
379
	, cachekey      :: Maybe String
380 381 382 383 384 385 386 387 388 389 390 391
	, response_code :: Int
	, results       :: Int
	}

derive JSONEncode LogEntry

log :: LogMessage` (Maybe LogMemory) *World -> *(Maybe LogMemory, *World)
log msg mem w
# mem     = fromJust (mem <|> pure zero)
# (mem,w) = updateMemory msg mem w
| not needslog = (Just mem, w)
# (io,w)  = stdio w
Camil Staps's avatar
Camil Staps committed
392
# io      = io <<< toString (toJSON $ makeLogEntry msg mem) <<< "\n"
393 394 395 396 397
= (Just mem, snd (fclose io w))
where
	needslog = case msg of (Sent _ _) = True; _ = False

	updateMemory :: LogMessage` LogMemory *World -> *(LogMemory, *World)
398 399
	updateMemory (Connected ip) s w = ({s & mem_ip=ip}, w)
	updateMemory (Received r)   s w
400 401
	# (t,w) = localTime w
	= ({s & mem_time_start=t, mem_request=r}, w)
402
	updateMemory (Sent _ _)     s w
403 404 405 406
	# (t,w) = localTime w
	= ({s & mem_time_end=t}, w)
	updateMemory _                   s w = (s,w)

Camil Staps's avatar
Camil Staps committed
407
	makeLogEntry :: LogMessage` LogMemory -> LogEntry
408
	makeLogEntry (Sent response (ck,us)) mem =
Camil Staps's avatar
Camil Staps committed
409
		{ ip            = toString mem.mem_ip
410 411
		, time_start    = (toString mem.mem_time_start, toInt $ timeGm mem.mem_time_start)
		, time_end      = (toString mem.mem_time_end, toInt $ timeGm mem.mem_time_end)
412
		, microseconds  = us
Camil Staps's avatar
Camil Staps committed
413 414 415 416 417
		, request       = mem.mem_request
		, cachekey      = ck
		, response_code = response.return
		, results       = length response.data
		}
Camil Staps's avatar
Camil Staps committed
418 419 420 421 422 423 424 425

err :: CloogleError String -> Response
err c m = { return         = toInt c
          , data           = []
          , msg            = m
          , more_available = Nothing
          , suggestions    = Nothing
          }