Skip to content

Commit de95f4b

Browse files
committed
chore: remove context receiver
1 parent 1ad552b commit de95f4b

File tree

6 files changed

+100
-102
lines changed

6 files changed

+100
-102
lines changed

backend/jvm/build.gradle.kts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -234,10 +234,10 @@ dependencies {
234234
testImplementation(libs.konsist)
235235

236236
// Copy js and wasm apps
237-
jsApp(project(path = projects.web.dependencyProject.path, configuration = "jsApp"))
238-
wasmApp(project(path = projects.web.dependencyProject.path, configuration = "wasmApp"))
237+
jsApp(project(path = projects.web.identityPath.path, configuration = "jsApp"))
238+
wasmApp(project(path = projects.web.identityPath.path, configuration = "wasmApp"))
239239
composeWebApp(
240-
project(path = projects.compose.cmp.dependencyProject.path, configuration = "composeWebApp"))
240+
project(path = projects.compose.cmp.identityPath.path, configuration = "composeWebApp"))
241241

242242
// Specify the classifier using variantOf
243243
// implementation(variantOf(libs.lwjgl.opengl) { classifier("natives-linux") })

backend/jvm/src/main/kotlin/dev/suresh/lang/FFM.kt

Lines changed: 17 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -10,19 +10,19 @@ import java.time.Instant
1010

1111
object FFM {
1212

13-
context(KLogger)
14-
suspend fun memoryLayout() = runOnVirtualThread {
15-
memoryAPIs()
16-
currTime()
17-
strlen("Hello Panama!")
18-
getPid()
19-
gmtime()
20-
terminal()
21-
dhReflection()
13+
suspend fun memoryLayout(logger: KLogger) = runOnVirtualThread {
14+
with(logger) {
15+
memoryAPIs()
16+
currTime()
17+
strlen("Hello Panama!")
18+
getPid()
19+
gmtime()
20+
terminal()
21+
dhReflection()
22+
}
2223
}
2324

24-
context(KLogger)
25-
private fun strlen(str: String) {
25+
private fun KLogger.strlen(str: String) {
2626
val strlenAddr = SYMBOL_LOOKUP.findOrNull("strlen")
2727
val strlenDescriptor = FunctionDescriptor.of(ValueLayout.JAVA_INT, AddressLayout.ADDRESS)
2828
val strlen = LINKER.downcallHandle(strlenAddr, strlenDescriptor)
@@ -33,8 +33,7 @@ object FFM {
3333
}
3434
}
3535

36-
context(KLogger)
37-
private fun currTime() {
36+
private fun KLogger.currTime() {
3837
// Print the current time.
3938
val timeAddr = SYMBOL_LOOKUP.findOrNull("time")
4039
val timeDesc = FunctionDescriptor.of(ValueLayout.JAVA_LONG)
@@ -43,8 +42,7 @@ object FFM {
4342
info { "time() = $timeResult epochSecond" }
4443
}
4544

46-
context(KLogger)
47-
private fun gmtime() {
45+
private fun KLogger.gmtime() {
4846
val gmtAddr = SYMBOL_LOOKUP.findOrNull("gmtime")
4947
val gmtDesc =
5048
FunctionDescriptor.of(
@@ -59,8 +57,7 @@ object FFM {
5957
}
6058
}
6159

62-
context(KLogger)
63-
private fun getPid() {
60+
private fun KLogger.getPid() {
6461
val getpidAddr = SYMBOL_LOOKUP.findOrNull("getpid")
6562
val getpidDesc = FunctionDescriptor.of(ValueLayout.JAVA_INT)
6663
val getpid = LINKER.downcallHandle(getpidAddr, getpidDesc)
@@ -79,8 +76,7 @@ object FFM {
7976
* } point = { 1.0, 2.0 };
8077
* ```
8178
*/
82-
context(KLogger)
83-
private fun memoryAPIs() {
79+
private fun KLogger.memoryAPIs() {
8480
Arena.ofConfined().use { arena ->
8581
val point = arena.allocate(ValueLayout.JAVA_DOUBLE.byteSize() * 2)
8682
point.set(ValueLayout.JAVA_DOUBLE, 0, 1.0)
@@ -133,8 +129,7 @@ object FFM {
133129
}
134130
}
135131

136-
context(KLogger)
137-
private fun terminal() {
132+
private fun KLogger.terminal() {
138133
val winsize =
139134
MemoryLayout.structLayout(
140135
ValueLayout.JAVA_SHORT.withName("ws_row"),
@@ -204,8 +199,7 @@ object FFM {
204199
}
205200

206201
/** Reflectively invoke the downcallHandle method on the Linker class. */
207-
context(KLogger)
208-
private fun dhReflection() {
202+
private fun KLogger.dhReflection() {
209203
val mh =
210204
MethodHandles.lookup()
211205
.findVirtual(

backend/jvm/src/main/kotlin/dev/suresh/lang/VThread.kt

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -14,24 +14,24 @@ import stdlibFeatures
1414

1515
object VThread {
1616

17-
context(KLogger)
18-
suspend fun virtualThreads() = runOnVirtualThread {
19-
info { (Greeting().greeting()) }
20-
listOf("main", "jvm", "js", "wasm").forEach {
21-
info { "Common-$it --> ${ClassLoader.getSystemResource("common-$it-res.txt")?.readText()}" }
22-
info { "Backend-$it --> ${ClassLoader.getSystemResource("backend-$it-res.txt")?.readText()}" }
23-
}
17+
suspend fun virtualThreads(logger: KLogger) = runOnVirtualThread {
18+
with(logger) {
19+
info { (Greeting().greeting()) }
20+
listOf("main", "jvm", "js", "wasm").forEach {
21+
info { "Common-$it : ${ClassLoader.getSystemResource("common-$it-res.txt")?.readText()}" }
22+
info { "Backend-$it : ${ClassLoader.getSystemResource("backend-$it-res.txt")?.readText()}" }
23+
}
2424

25-
structuredConcurrency()
26-
langFeatures()
27-
stdlibFeatures()
28-
kotlinxMetaData()
29-
classFileApi()
30-
info { "Concurrent Gatherers: ${gatherers().size}" }
25+
structuredConcurrency()
26+
langFeatures()
27+
stdlibFeatures()
28+
kotlinxMetaData()
29+
classFileApi()
30+
info { "Concurrent Gatherers: ${gatherers().size}" }
31+
}
3132
}
3233

33-
context(KLogger)
34-
private fun structuredConcurrency() {
34+
private fun KLogger.structuredConcurrency() {
3535
info { "Structured concurrency..." }
3636
val taskList =
3737
StructuredTaskScope<String>().use { sts ->
@@ -75,8 +75,7 @@ object VThread {
7575
return (1..100).toList().stream().gather(mapGatherer).toList()
7676
}
7777

78-
context(KLogger)
79-
private fun kotlinxMetaData() {
78+
private fun KLogger.kotlinxMetaData() {
8079
val metadataAnnotation = LocalDateTime::class.java.getAnnotation(Metadata::class.java)
8180
when (val metadata = KotlinClassMetadata.readLenient(metadataAnnotation)) {
8281
is KotlinClassMetadata.Class -> {

backend/jvm/src/main/kotlin/dev/suresh/routes/Service.kt

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,15 @@ import io.ktor.server.response.*
1414
import io.ktor.server.routing.*
1515
import io.ktor.server.sessions.*
1616
import io.opentelemetry.instrumentation.annotations.WithSpan
17-
import java.io.Writer
1817

1918
private val logger = KotlinLogging.logger {}
2019

2120
fun Routing.services() {
22-
get("/ffm") { call.respondLogStream { FFM.memoryLayout() } }
21+
get("/ffm") { call.respondLogStream { FFM.memoryLayout(this) } }
2322

24-
get("/vthreads") { call.respondLogStream { VThread.virtualThreads() } }
23+
get("/vthreads") { call.respondLogStream { VThread.virtualThreads(this) } }
2524

26-
get("/jfr") { call.respondLogStream { JFR.recordingStream() } }
25+
get("/jfr") { call.respondLogStream { JFR.recordingStream(this) } }
2726

2827
get("/trace") {
2928
call.respond(
@@ -47,9 +46,7 @@ fun Routing.services() {
4746

4847
suspend fun ApplicationCall.respondLogStream(
4948
contentType: ContentType = ContentType.Text.EventStream,
50-
block:
51-
suspend context(KLogger)
52-
Writer.() -> Unit
49+
block: suspend KLogger.() -> Unit
5350
) {
54-
respondTextWriter(contentType = contentType) { block(RespLogger(this, logger), this) }
51+
respondTextWriter(contentType = contentType) { block(RespLogger(this, logger)) }
5552
}

backend/jvm/src/main/resources/logback.xml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,9 +56,16 @@
5656
<totalSizeCap>1GB</totalSizeCap>
5757
</rollingPolicy>
5858
<encoder class="JsonEncoder">
59+
<withLoggerName>true</withLoggerName>
5960
<withFormattedMessage>true</withFormattedMessage>
6061
<withMessage>false</withMessage>
6162
<withArguments>false</withArguments>
63+
<withSequenceNumber>false</withSequenceNumber>
64+
<withContext>false</withContext>
65+
<withNanoseconds>false</withNanoseconds>
66+
<withMDC>false</withMDC>
67+
<withTimestamp>true</withTimestamp>
68+
<withThrowable>false</withThrowable>
6269
</encoder>
6370
</appender>
6471

backend/profiling/src/main/kotlin/dev/suresh/JFR.kt

Lines changed: 52 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -25,67 +25,68 @@ object JFR {
2525

2626
private val started = AtomicBoolean(false)
2727

28-
context(KLogger)
29-
suspend fun recordingStream() = runOnVirtualThread {
30-
if (!started.getAndSet(true)) {
31-
info { "Adding periodic JFR event..." }
32-
addPeriodicJFREvent(Counter()) { inc() }
33-
}
28+
suspend fun recordingStream(logger: KLogger) = runOnVirtualThread {
29+
with(logger) {
30+
if (!started.getAndSet(true)) {
31+
info { "Adding periodic JFR event..." }
32+
addPeriodicJFREvent(Counter()) { inc() }
33+
}
3434

35-
val config = Configuration.getConfiguration("profile")
36-
RecordingStream(config).use {
37-
info { "RecordingStream started!" }
38-
it.setMaxSize(5_000_000)
39-
it.enable("jdk.CPULoad").withPeriod(Duration.ofSeconds(1))
40-
it.onEvent("jdk.CPULoad") { event ->
41-
val jvmUser = event.getFloat("jvmUser")
42-
val jvmSystem = event.getFloat("jvmSystem")
43-
val machineTotal = event.getFloat("machineTotal")
44-
info {
45-
"JVM User: %1$.2f, JVM System: %2$.2f, Machine Total: %3$.2f"
46-
.format(jvmUser, jvmSystem, machineTotal)
47-
}
35+
val config = Configuration.getConfiguration("profile")
36+
RecordingStream(config).use {
37+
info { "RecordingStream started!" }
38+
it.setMaxSize(5_000_000)
39+
it.enable("jdk.CPULoad").withPeriod(Duration.ofSeconds(1))
40+
it.onEvent("jdk.CPULoad") { event ->
41+
val jvmUser = event.getFloat("jvmUser")
42+
val jvmSystem = event.getFloat("jvmSystem")
43+
val machineTotal = event.getFloat("machineTotal")
44+
info {
45+
"JVM User: %1$.2f, JVM System: %2$.2f, Machine Total: %3$.2f"
46+
.format(jvmUser, jvmSystem, machineTotal)
47+
}
4848

49-
// if (jvmUser > 0.8) {
50-
// it.dump(Path("cpu-load.jfr"))
51-
// }
52-
}
49+
// if (jvmUser > 0.8) {
50+
// it.dump(Path("cpu-load.jfr"))
51+
// }
52+
}
5353

54-
// Contended classes for more than 10ms
55-
it.enable("jdk.JavaMonitorEnter").withThreshold(Duration.ofMillis(10))
56-
it.onEvent("jdk.JavaMonitorEnter") { event ->
57-
info { "Long held Monitor: ${event.getClass("monitorClass")}" }
58-
}
54+
// Contended classes for more than 10ms
55+
it.enable("jdk.JavaMonitorEnter").withThreshold(Duration.ofMillis(10))
56+
it.onEvent("jdk.JavaMonitorEnter") { event ->
57+
info { "Long held Monitor: ${event.getClass("monitorClass")}" }
58+
}
5959

60-
it.enable("jdk.GarbageCollection")
61-
it.enable("jdk.JVMInformation")
62-
it.onEvent("jdk.JVMInformation") { event ->
63-
val jvmName = event.getString("jvmName")
64-
val jvmVersion = event.getString("jvmVersion")
65-
info { "JVM: $jvmName, Version: $jvmVersion" }
66-
}
60+
it.enable("jdk.GarbageCollection")
61+
it.enable("jdk.JVMInformation")
62+
it.onEvent("jdk.JVMInformation") { event ->
63+
val jvmName = event.getString("jvmName")
64+
val jvmVersion = event.getString("jvmVersion")
65+
info { "JVM: $jvmName, Version: $jvmVersion" }
66+
}
6767

68-
it.enable("dev.suresh.Counter")
69-
it.onEvent("dev.suresh.Counter") { event ->
70-
val duration = event.duration.toMillis()
71-
info { "Count: ${event.getLong("count")}, duration: $duration" }
68+
it.enable("dev.suresh.Counter")
69+
it.onEvent("dev.suresh.Counter") { event ->
70+
val duration = event.duration.toMillis()
71+
info { "Count: ${event.getLong("count")}, duration: $duration" }
7272

73-
// Find correlation events by getting an event window 1 sec before and after the event.
74-
if (duration > 500) {
75-
EventStream.openRepository().use { es ->
76-
es.setStartTime(event.startTime.minus(Duration.ofSeconds(1)))
77-
es.setEndTime(event.endTime.plus(Duration.ofSeconds(1)))
78-
es.onEvent("jdk.GCPhasePause") { gcEvent ->
79-
val gcDuration = gcEvent.duration.toMillis()
80-
info { "GC pause of $gcDuration millis during the Counter event!" }
73+
// Find correlation events by getting an event window 1 sec before and after the event.
74+
if (duration > 500) {
75+
EventStream.openRepository().use { es ->
76+
es.setStartTime(event.startTime.minus(Duration.ofSeconds(1)))
77+
es.setEndTime(event.endTime.plus(Duration.ofSeconds(1)))
78+
es.onEvent("jdk.GCPhasePause") { gcEvent ->
79+
val gcDuration = gcEvent.duration.toMillis()
80+
info { "GC pause of $gcDuration millis during the Counter event!" }
81+
}
8182
}
8283
}
8384
}
84-
}
8585

86-
it.startAsync()
87-
Thread.sleep(3.seconds.toJavaDuration())
86+
it.startAsync()
87+
Thread.sleep(3.seconds.toJavaDuration())
88+
}
89+
info { "RecordingStream done!" }
8890
}
89-
info { "RecordingStream done!" }
9091
}
9192
}

0 commit comments

Comments
 (0)